1

I'm trying to count results for a given query, but I don't actually need the posts so I'm trying to figure out if there's a way to count posts without querying them. Something similar to https://codex.wordpress.org/Function_Reference/wp_count_posts

I'm counting posts by meta key/value so wp_count_posts() won't work.

Here's what I'm currently doing:

$query = new WP_Query(array(
  "post_type" => 'my-post-type',
  "posts_per_page" => 0,
  "meta_key" => "my_custom_key",
  "meta_value" => "some_value",
));

echo $query->found_posts;
2
  • not sure what do you mean in "without doing a query". wp_count_posts does a query.... May 8, 2017 at 14:39
  • @MarkKaplun right, looking at the source it does a custom $wpdb query (github.com/WordPress/WordPress/blob/…). What I mean with "without doing a query" is without setting up a new WP_Query and executing it. What I'm trying to get at is a simple way to count posts with minimal query time overhead.
    – Ahrengot
    May 8, 2017 at 19:31

2 Answers 2

3

The non perfect solution is to just minimize the amount of information carried by the query, and the side effects. This can be done by requesting only the post IDs and not populating any caches.

Your query should be something like

$query = new WP_Query(array(
  ...
  'fields' => 'ids',
  'cache_results' => false,
  'update_post_meta_cache' => false,
  'update_post_term_cache' => false,
));

So you still get too much information - an array of integers instead of one, but since the DB has to go over them in any case the main overhead is probably the time to transfer it, and if you do not expect 1000s of posts matching your query, then it should be good enough.

The reason I would prefer this approach over trying to figure out all the required joins and write a proper SQL, is that it is much easier to read and modify than an SQL statement. Still requires a good comment about the why of it.

5
  • 1
    I believe by using fields => ids WP_Query will actually skip over any normal WP_Query caching features.
    – Howdy_McGee
    May 8, 2017 at 20:10
  • most likely... you probably investigated it more than me :) May 8, 2017 at 20:24
  • Wait - Is it faster NOT to cache queries? This is news to me :)
    – Ahrengot
    May 8, 2017 at 20:35
  • @Ahrengot, it is not about caching queries, it is about caching the meta and term info that is done "automatically" when a query is done, and in your case, if you really want just the count, there is no reason to spend the time on doing them. .... and even just caching has its own cost especially if you are using an external server (like when using memcache in distributed enviroment) for your object caching. May 8, 2017 at 20:45
  • 1
    @Howdy_McGee, ok I see it now in the code, obviously not documented anywhere :( May 8, 2017 at 20:48
-1

You set posts_per_page 0 value. posts_per_page for all posts set -1 or another positive number

$query = new WP_Query( array(
  "post_type"       => 'my-post-type',
  "posts_per_page"  => -1,
  "meta_key"        => "my_custom_key",
  "meta_value"      => "some_value",
) );

echo $query->found_posts;
1
  • My code above works as intended. $query->found_posts counts all matches, not just those in $query->posts. What I'm asking for is a way to do the above without having to do a WP_Query.
    – Ahrengot
    May 8, 2017 at 14:38

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.