I’m trying to run a query that returns all the posts with a given value for a specific meta key. I know for a fact that this is none of the posts on my test server but it reports finding all the posts with a post count of 7 (there are 21 posts).
I have been following the notes here and the question here but I cannot get a sensible outcome.
$args = array(
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',// outcome is the same with and without this line
array(
'key' => 'perma',
'compare' => '=',
'value' => "{$raw_perma}"
),
),
);
// create a custom query
$my_query = new WP_Query( $args );
echo $my_query->post_count; // 7 (regardless of $raw_perma value).
I would like to know what I am doing wrong. As “are there posts with this meta key-value pair?” seems like it should be a trivial question to answer.
Update
Found another question and tried this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'perma',
'meta_value' => "{$raw_perma}"
);
This works although I have no idea why this and not that. Would like to see an explanation in an answer.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This works although I have no idea why this and not that. Would like to see an explanation in an answer.
Because that code sets the posts_per_page value to -1 which means no limit and it’s similar to setting nopaging to true, hence all found posts are returned.
it reports finding all the posts with a post count of 7 (there are 21 posts)
I think the original issue here is that you probably not aware that the property $post_count (i.e. WP_Query::$post_count) is actually the number of posts just for the current page, so if LIMIT was used (in the SQL statement generated by WP_Query), e.g. by setting the posts_per_page value to, say, 10 which is the default value, then the $post_count would be 10 or less depending on the total number of posts in the database which matched the specific query.
So if you want to get that total number, then use the $found_posts property:
$my_query = new WP_Query( array(
'posts_per_page' => 1, // just for testing
) );
echo "Number of posts with LIMIT applied: {$my_query->post_count}<br>";
echo "Number of posts without LIMIT applied: {$my_query->found_posts}";
See here if you’d like to check the other properties in WP_Query.
Additional Notes
-
If you don’t set the
posts_per_pagein your query args, the value will default to10or whatever you put in the “Blog pages show at most” input on the Reading Settings admin page (wp-admin→ Settings → Reading). And I guessed you had it set to7? -
I think that
"{$raw_perma}"could simply be written as$raw_permaas in'meta_value' => $raw_permaand'value' => $raw_perma.. 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0