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:
- In the category provided (
$catID
) - Must have a field called
im_featured_on_category_pages
im_featured_on_category_pages
must not be emptyim_featured_on_category_pages
must contain ('LIKE') the$catID
- 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?