Home › Forums › General Issues › Numberposts & Meta Query Not Working
Hi all, I’m trying to execute a query that only pulls the 3 latest posts from the “case_study” post type, using the following args for a WP_Query. However, the code seems to pull the 5 latest posts no matter what I do. I’ve tried using ‘numberposts’ and that doesn’t work either. Moreover, it is completely ignoring the key and value qualifiers I’m passing with the query arguments. Not sure what I’m doing wrong here. Any help would be greatly appreciated.
$relatedCat = get_field(‘service_related_industry’);
$args = array (
‘posts_per_page’ => 3,
‘post_type’ => ‘case_study’,
‘meta_query’ => array(
‘key’ => ‘cs_industry’,
‘value’ => $relatedCat,
‘compare’ => ‘LIKE’,
),
‘orderby’ => ‘DESC’,
);
For the second part, meta_query must be a nested array, like this
'meta_query' => array(
array(
'key' => 'cs_industry',
'value' => $relatedCat,
'compare' => 'LIKE',
)
),
for why it’s showing 5 instead of 3, this could be that your posts_per_page value is being overridden by a pre_get_posts filter, or something similar.
Thanks for helping, John. However, the nested array didn’t solve the issue. It’s still just showing the latest posts without filtering based on value.
Also, there isn’t any other code on the page determining how many posts should be pulled, not that I can find for this query. Is there a way to make this one override any others?
To clarify further, the whole code snippet is as follows:
$relatedCat = get_field('service_related_industry');
$args = array (
'posts_per_page' => 3,
'post_type' => 'case_study',
'meta_query' => array(
array(
'key' => 'cs_industry',
'value' => $relatedCat,
'compare' => 'LIKE',
)
),
'orderby' => 'DESC',
);
if (!$relatedCat) {
$relatedCat = get_field('service_related_category');
unset ($args);
$args = array (
'posts_per_page' => 3,
'cat' => $relatedCat,
'orderby' => 'DESC',
);
}
If I comment out the first line, the IF statement works perfectly, and in fact it also adjusts the number of posts correctly. So, I reckon the problem is in the way the first field (service_related_industry) is being handled. But I can’t quite figure out what’s going wrong.
That’s the thing with WP hooks, filters and actions. There could be code in another file that alters every query done by WP. This is just one example: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
But then that wouldn’t explain why the second query works properly. If there were code elsewhere that was changing the number of posts shown, surely it would affect both conditions – before and after the IF statement.
Your first set of $args is querying the CPT “case_study”, the second set of $args is querying the default post type of “post”. This is a significant difference.
Sorry if I’m being thick, but the first $arg references a brand new ACF field that’s not referenced anywhere else, so I can’t imagine that anything outside of the code on this page could be influencing its variables.
Is there a way to override settings from elsewhere?
There is one thing I just noticed as far as the meta_query goes.
First of all, if you’re field is not returning an ID of the “ct_industry” assuming that this is some type of relationship field, then you need to get the unformatted value.
Secondly, since you are searching for numbers in a field that contains arrays then your LIKE needs to be altered.
All of the following are guesses assuming that “ct_industry” is either a taxonomy or a relationship field.
$relatedCat = get_field('service_related_industry', false, false);
$args = array (
'posts_per_page' => 3,
'post_type' => 'case_study',
'meta_query' => array(
array(
'key' => 'cs_industry',
'value' => '"'.$relatedCat.'"',
'compare' => 'LIKE',
)
),
'orderby' => 'DESC',
);
if (!$relatedCat) {
$relatedCat = get_field('service_related_category');
unset ($args);
$args = array (
'posts_per_page' => 3,
'cat' => $relatedCat,
'orderby' => 'DESC',
);
}
as far as why it’s returning the wrong number of posts, yes, a pre_get_post filter added somewhere else can alter the settings of any query done anywhere and any time https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
First of all, thank you for your continued help. I really appreciate it.
I tried the code you suggested, but haven’t yet solved either of the issues. I feel like we’re getting closer to finding the issue.
The cs_industry is a taxonomy relating to the custom post type “case study”. And the variable $relatedCat does give a number (e.g. 1079).
When I print_r($args), I get:
Array ( [posts_per_page] => 3 [post_type] => case_study [meta_query] => Array ( [0] => Array ( [key] => cs_industry [value] => "1079" [compare] => LIKE ) ) [orderby] => DESC )
cs_industry is a taxonomy attached to the custom post type ‘case_study’. It does seem to save the values I tick for each post. Not sure if that answers your question?
in the settings for the field there are two choices, one for load terms and one for set terms. If you have these checked then ACF associates the terms with the posts, just like categories and tags work for regular posts. If this is the case then try using a tax_query instead of a meta_query.
Your question has made me realise that the issue is probably not with the ACF part but the Custom Post Types taxonomy I was querying.
get_field is correctly pulling the field ID. However, my $args was not querying for posts filtered by taxonomy. I’ve changed the code to:
<?php $relatedCat = get_field('service_related_industry');
$args = array (
'posts_per_page' => 3,
'post_type' => 'case_study',
'tag_id' => '"'.$relatedCat.'"',
'orderby' => 'DESC',
It’s still not working properly, but at least now it isn’t pulling all the posts (it’s pulling exactly the 3 latest, while ignoring the ‘tag_id’ filter).
OK… I finally got it. This is what the correct query should have been, combining ACF and CPT arguments:
$relatedCat = get_field('service_related_industry');
$args = array (
'posts_per_page' => 3,
'post_type' => 'case_study',
'tax_query' => array(
array(
'taxonomy' => 'cs_industry',
'terms' => $relatedCat,
'field' => 'term_id',
),
),
'tag_id' => '"'.$relatedCat.'"',
'orderby' => 'DESC',
);
Completely thanks to all your help! The answer was basically found by putting together most of your suggestions and identifying correct variables to work with. Even the last bit you pointed me in the right direction. Thanks again, very much!
The topic ‘Numberposts & Meta Query Not Working’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.