WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a fast way to get the count (number) of post children of a certain post? (The post has a custom post type)

I don't want to use WP_Query for this because I don't need all the extra data...

LE: based on wyrfel's answer I ended up using:

function reply_count($topic, $type = 'topic-reply'){
  global $wpdb;
  $cache_key = "{$type}_{$topic}_counts";
  $count = wp_cache_get($cache_key, 'counts');
  if(false !== $count) return $count;
  $query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s";
  $count = $wpdb->get_var($wpdb->prepare($query, $topic, $type));
  wp_cache_set($cache_key, $count, 'counts');
  return $count;
share|improve this question
fastest way would be to create your own sql select count. – Bainternet Feb 15 '11 at 11:14
Aye...only seeing this now...nice. – wyrfel Feb 15 '11 at 20:28
up vote 3 down vote accepted
class MyClass {


    function post_count_filter($where) {
        global $wpdb;
        str_replace("WHERE", "WHERE ".$wpdb->posts.".post_parent = ".$this->count_parent." AND", $where);
        return $where;

    function count_children($post_id) {
        $this->count_parent = $post_id;
        add_filter('query', ( &$this, 'post_count_filter') );
        $stats = wp_count_posts('myposttype');
        remove_filter('query', ( &$this, 'post_count_filter') );
        return array_sum( (array)$stats );

    // example of use
    function xyz() {
        global $post;
        $child_count = $this->count_children($post->ID);



The only issue with that is that wp_count_posts() caches its results and since your filter bypasses the cache you might have to devalidate the cache, first.

Or (better), copy the wp_count_posts() function and modify it to your needs, so you don't have to use the filter, don't have to sum up the results and avoid having the rest done that it does.

share|improve this answer
thanks. cache should be useful. I think cache plugins flush this after a post is created... – onetrickpony Feb 15 '11 at 16:07
Yes, but your trouble will be that the cache identifier used by wp_count_posts() is not aware of the fact that you're changing the query. So you may get the same cached count values back in the count_children() function that other wp_count_posts() calls - that are NOT filtering by post_parent - retrieved from the db. To really be on the safe side - and get another bit of performance improvement - write a function that runs your own SQL query. I wish there was a count posts function that would parse the query args and has a per-argset cache. – wyrfel Feb 15 '11 at 20:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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