Home › Forums › General Issues › Relation field meta_query seemingly ignored
Hi,
I have a (category) relation custom field in my posts called im_featured_on_category_pages — basically it allows an admin to select if the post should be “featured” on the selected category pages.
In a function called get_category_featured($catID) inside my functions.php file, I have a function with the following get_posts query:
$cat_featured_posts = get_posts([
'numberposts' => 3,
'post_type' => 'post',
'category' => $catID,
'meta_query' => [
'relation' => 'AND',
[
'key' => 'im_featured_on_category_pages',
'compare' => 'EXISTS'
],
[
'key' => 'im_featured_on_category_pages',
'value' => '',
'compare' => '!='
],
[
'key' => 'im_featured_on_category_pages',
'value' => '"' . $catID . '"',
'compare' => 'LIKE'
]
],
'orderby' => 'date',
'order' => 'DESC'
]);
This query works…it only gets 3 posts within that category…but…the meta_query is seemingly ignored. It still returns all posts whether or not the ACF field exists, is empty, or has the correct value I’m looking for.
Basically I want the returned posts to be:
1) In the category provided (catID)
2) Must have a field called im_featured_on_category_pages
3) im_featured_on_category_pages must not be empty
4) im_featured_on_category_pages must contain (‘LIKE’) the $catID
5) The 3 posts returned must be ordered by date (newest first)
Any help at this point would be greatly appreciated!
UPDATE: So I’ve tried several different queries, and no matter what I put in the meta_query
…it seems to be ignored…I even took the meta_query
out and am getting the same result without it. It’s like get_posts()
is completely ignoring my meta_query
. Does that have anything to do with the fact that I’m using it in functions.php
and not in a template?
Sorry guys…I actually inherited this install from someone else. The problem was a plugin (BEEP, https://wordpress.org/plugins-wp/brilliant-easy-exclude-posts/)…It was causing my meta_queries not to work at all. I’m going to file a report with the plugin folks, but it looks like they aren’t maintaining it anymore anyway. Hope this helps someone along the way :/
The topic ‘Relation field meta_query seemingly ignored’ is closed to new replies.
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.