I have a (category) relation custom field (ACF) 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. The stored value is a serialized array like a:1:{i:0;s:3:"273";} where '273' would be a "featured" category ID.

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...been at this for about 8 hours and can't figure out what I'm doing wrong here.

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?

1 Answer 1


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.

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.