0

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.

7
  • 1
    Why are you using 'relation' => 'AND', for the meta_query?
    – Q Studio
    Jan 19 '21 at 16:10
  • @QStudio in the hope that it might work. I have got to the point where I trying daft things because I just don't know what the issue is. Jan 19 '21 at 16:19
  • 1
    Perhaps go back to basics, and show the code you think should work - also show how $raw_perma is generated, and for good measures, make a SQL query directly to phpmyadmin showing the right / expected results - to ensure this is not a data issue.
    – Q Studio
    Jan 19 '21 at 16:55
  • I have had a PHPMyAdmin tab open the entire time. It's not a data issue as far as I can tell. Jan 19 '21 at 17:06
  • 1
    Guessing, remove the AND and remove the compare - this is redundant - and read this post, might help: rudrastyh.com/wordpress/meta_query.html
    – Q Studio
    Jan 19 '21 at 17:36
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

  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 .. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.