• Resolved treschlet

    (@treschlet)


    One more question for today!

    I’d like to run a get_posts query, and I’d like to specify multiple conditions based on multiple custom fields.

    Namely something like:
    ‘boolean_field’ = ‘1’
    ‘date_start_field’ <= current_date
    ‘date_end_field’ >= end date

    Is there a way to do this? currently all I can get to work is
    $args[‘meta_key’]
    $args[‘meta_value’] = ‘1’

    but that of course only works for one field. I was trying to do something like
    $args[‘boolean_field’] = 1;
    $args[‘date_start_field’][‘le’] = currend_date

    etc, but without the ‘meta_key’ specified nothing ever returns (the get_posts documentation seems to imply that this should work, but it does not)

    note: no I’m not actually using ‘boolean_field’ as a field name, this is just for example 🙂

    https://wordpress.org/plugins/custom-content-type-manager/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    The “between” operator is tricky so it’s not supported directly. See https://code.google.com/p/wordpress-custom-content-type-manager/wiki/get_posts#Operators

    There are, however, workarounds. E.g. you can get all the posts posted in a given month (or day or hour etc) using “starts_with”:

    $args['post_date']['starts_with'] = '2014-04';

    You can set the “date_column” argument and then use the “date_min” and “date_max” arguments to specify an arbitrary range:

    $args['date_column'] = 'my_custom_field';
    $args['date_min'] = '2013-03-13';
    $args['date_max'] = '2014-04-14';
    Thread Starter treschlet

    (@treschlet)

    Actually, what I’m really having trouble with now that I’ve fiddled with it some more, is the custom field queries not really working as expected. Maybe you can help shed some light.

    //only returns posts with boolean_field checked
    $query['boolean_field'] = '1';

    //returns ALL POSTS regardless of what’s checked
    $query['boolean_field'] = '0';
    or
    $query['boolean_field'] = '';

    I can’t seem to just do a simple exclusion of a post from the query based on a checkbox. Is this working as intended?

    Thread Starter treschlet

    (@treschlet)

    I should also mention that these options don’t seem to work as expected either:

    //returns Posts where boolean_field = 1 (expected would be the opposite)

    $query['boolean_field']['!='] = 1
    $query['boolean_field']['ne'] = 1

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Try debugging the query using the debug option — if there’s a bug in the query generation, then please file a bug. It could be that null/zero values are not being stored in the database at all, in which case the query would fail: you’ll have to look at the raw data to determine that though.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_posts multiple meta_keys?’ is closed to new replies.