I'm trying to pass an array of values into a meta_query value.
// Works as expected to return posts with 1474 in featured
'meta_query' => array(
array(
'key' => 'featured',
'value' => 1474,
'compare' => 'LIKE'
)
)
// Works as expected to return posts with 2213 in featured
'meta_query' => array(
array(
'key' => 'featured',
'value' => 2213,
'compare' => 'LIKE'
)
)
However when I set as an array it does not work correctly. I am trying to return posts with 'featured' set to either 1474 or 2213.
// Does not work, wp_query returns all posts
'meta_query' => array(
array(
'key' => 'featured',
'value' => array(1474, 2213),
'compare' => 'LIKE'
)
)
I have also changed LIKE to IN with no success. I would like to stay away from multiple meta_query statements as the length of my array may change.
I'm trying to pass an array of values into a meta_query value.
// Works as expected to return posts with 1474 in featured
'meta_query' => array(
array(
'key' => 'featured',
'value' => 1474,
'compare' => 'LIKE'
)
)
// Works as expected to return posts with 2213 in featured
'meta_query' => array(
array(
'key' => 'featured',
'value' => 2213,
'compare' => 'LIKE'
)
)
However when I set as an array it does not work correctly. I am trying to return posts with 'featured' set to either 1474 or 2213.
// Does not work, wp_query returns all posts
'meta_query' => array(
array(
'key' => 'featured',
'value' => array(1474, 2213),
'compare' => 'LIKE'
)
)
I have also changed LIKE to IN with no success. I would like to stay away from multiple meta_query statements as the length of my array may change.
I manually added a "featured" meta key to 2 different posts with values of 1474 and 2213 and then ran a basic WP_Query() like so:
$qv = [
'meta_query' =>[
[
'key' => 'featured',
'value' => [1474, 2213],
'compare' => 'IN',
]
]
];
$r = new WP_Query($qv);
It pulled up both posts. This was query it generated:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND (
( wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value IN ('1474','2213') )
)
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 12
var_dump()
out your WP_Query()
and see what sql query WP is running.
IN
compare? – Howdy_McGee ♦ Commented Apr 24, 2020 at 20:23featured
? – Jacob Peattie Commented Apr 25, 2020 at 3:04