Question

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.

Was it helpful?

Solution

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

  1. If you don't set the posts_per_page in your query args, the value will default to 10 or 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 to 7?

  2. I think that "{$raw_perma}" could simply be written as $raw_perma as in 'meta_value' => $raw_perma and 'value' => $raw_perma .. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top