Return only Count from a wp_query request?

All we need is an easy explanation of the problem, so here it is.

Is it possible with the standard build-in tools in WordPress to get wp_query to only return the count of an query?

Right now, I’ve a query which has several meta_queries but what the only thing I’m interested in is the actually count of the query.

I know that I can use the found_posts property but the query itself generates a big overhead by query SELECT * and thus returns the whole object.

I could just as easy query the DB with a custom query using $wpdb but I would like to utilize the build-in query system if possible..

I’ve been looking for the answer for this on SE and Google but came on empty.

If I’ve explained myself poorly, please let me know and I’ll try to elaborate.

Cheers

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There is no build in function to achieve what you want, at least not for complicated meta queries like this. If you need to use build in functions for this, the best will be to make use of WP_Query.

To make the query faster and to skip the unwanted returned array of WP_Post properties, and because you are only interested in post count, you can use the following in your parameters in your arguments

'fields' => 'ids',
'no_found_rows' => true,

This might even be a bit faster than a custom SQL query, and the results from WP_Query is cached as well.

Method 2

I know this has been answered and since your question was based off on using WP_Query, my answer is probably a long-shot but you could also try to use this:

wp_count_posts( $type, $perm );

where $type = post_type (post, page, 'custom-post-type-slug') and where $perm = To include private posts readable by the current user, set to ‘readable’

I’ve used this in the past with great success 🙂

wp_count_posts('post')->publish -> returns only the count for published posts.

Method 3

If you need a complete example – building on Pieter’s notes:

$query = new WP_Query(
    array(
        'post_type' => 'post_type_goes_here',
        'posts_per_page' => -1,
        'no_found_rows' => true,
        'fields' => 'ids',
    )
);

if ($query->have_posts()) echo count($query->posts);
else echo "0";

Method 4

I would write a simple function that would return the posts count as well as the posts (based on @Pieter’s answer)

function get_posts_with_count($query_args, $page, $per_page)
{
    // count posts
    $posts_query = new WP_Query(
        $query_args,
    );
    $posts_count = count($posts_query->get_posts());
    $max_pages = ceil($posts_count / $page);

    // now actually get the posts
    $query_args['posts_per_page'] = $per_page;
    $query_args['paged'] = $page;
    unset($query_args['fields']);
    $posts_query = new WP_Query(
        $query_args
    );
    $posts = $posts_query->get_posts();

    return array(
        'posts_count' => $posts_count,
        'max_pages' => $max_pages,
        'data' => $posts
    );
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

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

Leave a Reply