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' => $filter_region,
'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.
I think you have to compare with IN instead of LIKE. The
value
must contain an array then.Here’s the doc.
You can use compare. Here is another WordPress Codex doc on Meta_Query.