1

I'm comparing the trip_region custom field of my trip CPT to the first element of an array: $filter_region[0].

Howevere I may have more elements in that array and I need to select trips whose trip_region field equals any of these elements. Is it possible, with meta_query, to compare (with an OR, not an AND) a field to several values at once? Because there's nothing quite like that in the doc.

$get_trips_region = get_posts(array(
    'post_type' => 'trip',
    'meta_query' => array(
        array(
            'key' => 'trip_region', 
            'value' => '"' . $filter_region[0] . '"', 
            'compare' => 'LIKE'
        )
    )
));

EDIT:

Here's my new code, presumably closer to the solution but doesn't return any result:

$get_trips_region = get_posts(array(
    'post_type' => 'trip',
    'meta_query' => array(
        array(
            'key' => 'trip_region', 
            'value' => array($filter_region[0], $filter_region[1]), 
            'compare' => 'IN'
        )
    )
));

EDIT(2):

Replacing get_posts with WP_Query and using WP_Query's request property I see the following SQL query is being executed here:

SELECT SQL_CALC_FOUND_ROWS wp_15_posts.ID FROM wp_15_posts INNER JOIN wp_15_postmeta ON (wp_15_posts.ID = wp_15_postmeta.post_id) WHERE 1=1 AND wp_15_posts.post_type = 'trip' AND (wp_15_posts.post_status = 'publish' OR wp_15_posts.post_status = 'private') AND ( (wp_15_postmeta.meta_key = 'trip_region' AND CAST(wp_15_postmeta.meta_value AS CHAR) IN ('47','46','45')) ) GROUP BY wp_15_posts.ID ORDER BY wp_15_posts.post_date DESC LIMIT 0, 10

Relevant passage I guess is: (wp_15_postmeta.meta_key = 'trip_region' AND CAST(wp_15_postmeta.meta_value AS CHAR) IN ('47','46','45'))

Looks good to me, I don't understand why it doesn't work. The same passage with the working version: LIKE '%\"45\"%'

1 Answer 1

1

The parameter 'meta_query' is an array, so you can add multiple meta keys. In the doc, below the section "Example: Multiple Meta Entries - Multi dimensional array", you can find an example where the relation is set to 'OR':

$query_args = array( 'meta_query' => array(
'relation' => 'OR',
array(
    'key' => 'foo_key',
    // 'value' => 'foo',
    // 'compare' => 'LIKE',
),
array(
    'key' => 'bar_key',
),
) );
[...]

The result query will then be:

'join' => string ' INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id' (length=60)
'where' => string ' AND (wp_postmeta.meta_key = 'foo_key' OR wp_postmeta.meta_key = 'bar_key' )'
(length=75)

If you want to check for multiple values with the same key, you can use an array and use 'IN' to compare:

array(
    'key' => 'foo_key',
    'value' => array('foo', 'bar'),
    'compare' => 'IN'
),
5
  • Thanks. I don't get any result now though. Any idea what I'm doing wrong? See edited question.
    – drake035
    Commented Jan 18, 2015 at 14:02
  • Are you sure there are posts matching the search criteria? Is it working when you try it with only one $filter_region value? Commented Jan 18, 2015 at 14:19
  • Yes, it works when I write: 'value' => '"' . $filter_region . '"'. Please check my second edit with the SQL query revealed.
    – drake035
    Commented Jan 18, 2015 at 17:44
  • I'm suspecting the problem as something to do with the fact that 'trip_region' is a "relationship" type of custom field created with Advanced Custom Field.
    – drake035
    Commented Jan 18, 2015 at 22:46
  • That's strange. Does the database structure looks the same when using 'normal' meta values? Have you tried executing the query in PHPMyAdmin? Another thing you can try is the first code example I send, and then use multiple arrays with the same key, only different values. Commented Jan 19, 2015 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.