Return only Count from a wp_query request?

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

Topic mysql count wp-query wpdb query Wordpress

Category Web


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
    );
}

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";

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.

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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.