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

I selected only 2 levels of comment for my wordpress site. How can I display the number of comment of the first level ? (Depth-1)

share|improve this question
Where did you select to "use" two levels of comments? Are you trying to retrieve the number of 1st-level comments while ignoring (not counting) all of those beneath the 1st level? Where are you trying to display this number? – bosco Jan 7 '13 at 23:52

3 Answers 3

In the wp_comments table, if comment_parent equals 0, it's a first-level comment.

function get_num_toplevel_comments() {
    global $wpdb;

    return $wpdb->get_var("
        SELECT COUNT(*) 
        FROM $wpdb->comments
        WHERE comment_parent = 0
share|improve this answer
@toscho - sorry to comment here, I'm not yet able to comment elsewhere. Your function retrieves all of the top-level comments in their entirety. @ Mathieu specifically asked in two separate places in the question for just the number of top-level comments. Your answer did teach me a new hook though! Virtual +1 :) – akTed Jan 8 '13 at 11:05

Use the code from this answer and remove just one !:

add_filter( 'comments_clauses', 'wpse_78628_top_comments_only' );

$comments = get_comments();

remove_filter( 'comments_clauses', 'wpse_78628_top_comments_only' );

function wpse_78628_top_comments_only( $clauses )
    $clauses['where'] .= ' AND comment_parent = 0';
    return $clauses;
share|improve this answer
Fantastic work.:) worked like a charm :) – Rohit Pande Jan 8 '13 at 10:26

here is some full working code, based on previous answers.

add_filter( 'comments_clauses', 'wpse_78490_child_comments_only' );

function wpse_78490_child_comments_only( $clauses )
    $clauses['where'] .= ' AND comment_parent != 0';
    return $clauses;
  $count = get_comments( array('post_id' => $post->ID, 'count' => true) ); echo $count; ?>
share|improve this answer

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.