Home › Forums › General Issues › Creating WP query with meta_query on array
On my post pages, I have created a Post Objects field that allows multiple items from a custom post type (called “Destinations”) to be associated with the post. I set the “Return Format” to be “Post ID”.
On the “Destination” custom post type single template, I wrote this query, which works well:
$destid = get_the_ID();
$postsargs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destinations',
'value' => $destid,
'compare' => 'LIKE',
)
)
);
$getposts = get_posts( $postsargs );
It successfully returns posts that have that particular Destination in the Post Object field.
I also want to run this query on a custom taxonomy archive page for the Destination post type. I put all of the custom post IDs with the current taxonomy term into an array called $destinations and then tried this query:
$postsargs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destinations',
'value' => $destinations,
'compare' => 'LIKE'
)
)
);
$getposts = get_posts( $postsargs );
But it returns all posts, not just the ones that have that Destination in the Post Object field.
How can I adjust this query on the Destination taxonomy archive so that it only gets posts where the Post Object field matches the Destinations from that term?
You cannot do a query on this field in that way. In fact, I’m surprised that you first query works correctly. It should be something like this
$postsargs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destinations',
// not the " around the ID
'value' => '"'.$destid.'"',
'compare' => 'LIKE'
)
)
);
$getposts = get_posts( $postsargs );
The only way that that your original query can work is if the ID number value is unique within the serialized array stored in the DB.
ACF stores the field as a serialized array. To query for mulitple values requires doing something like this
$postsargs = array(
'posts_per_page' => 5,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'destinations',
'value' => '"'.$destinations_1_id.'"',
'compare' => 'LIKE'
),
array(
'key' => 'destinations',
'value' => '"'.$destinations_2_id.'"',
'compare' => 'LIKE'
),
// etc for each destination
)
);
$getposts = get_posts( $postsargs );
With a lot of destinations this query will cause performance issues. My suggestion would be to set up a bidirectional relationship field for these posts.
Thanks for the suggestion, I got it working with the multiple arrays. But I do see how this could cause performance issues and will look at the bidirectional relationship field. Thanks your help!
Hello!
I got exactly the same problem while user “in” operator in meta_query with an array.
After 2 hours of research I found the solution!
The key “value” as to be “values” (with a S at the end) and the operator “IN” is going to work.
WORK:
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'centers',
'values' => $centres,
'compare' => 'in',
)
)
DOESN’T WORK
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'centers',
'value' => $centres,
'compare' => 'in',
)
)
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.