Jay Bro
Jay Bro

Reputation: 33

How to get replies count of individual comment in wordpress

I use traded comments so each comments receive multiple replies in it. I need to get replies count of each individual comment. I don't need the total replies count of all the comments.

I tried to use this code:

$comment_id = get_comment_ID();

$childrenCount = get_comment_meta( $comment_id, "childrenCount" );

$childrenCountNumber = count($childrenCount);

echo $childrenCountNumber

The other way round that shows total replies which not what I'm looking for.. This code shows all the replies of all the comments.`

Code in functions.php

function replies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments 
WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND 
`comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}

Then echo out in the template page:

$parents_count = replies_counter($post->ID);
$children_count = $post->comment_count - $parents_count;

echo $children_count;

This one is all about getting total replies count of all the comments. But I need replies count of each comment individually.

It will be nice if someone help me.

Upvotes: 1

Views: 2225

Answers (3)

Here are the code in comment.php

<?php
/**
 * The template for displaying comments.
 *
 * This is the template that displays the area of the page that contains both the current comments
 * and the comment form.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Astra
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

/*
 * If the current post is protected by a password and
 * the visitor has not yet entered the password we will
 * return early without loading the comments.
 */
if ( post_password_required() ) {
    return;
}
?>

<div id="comments" class="comments-area">
<h3>{<?php echo get_comments_number(); ?><span style="color:grey;font-size:16px">&nbsp;comments</span>}</h3>
    <?php astra_comments_before(); ?>

    <?php if ( have_comments() ) : ?>
        <div class="comments-count-wrapper">
            <h2 class="comments-title">
                <?php
                $comments_title = apply_filters(
                    'astra_comment_form_title',
                    sprintf( // WPCS: XSS OK.
                        /* translators: 1: number of comments */
                        esc_html( _nx( '%1$s thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', get_comments_number(), 'comments title', 'astra' ) ),
                        number_format_i18n( get_comments_number() ),
                        get_the_title()
                    )
                );

                echo esc_html( $comments_title );
                ?>
            </h2>
        </div>

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
        <nav id="comment-nav-above" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
            <h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
            <div class="nav-links">

                <div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
                <div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>

            </div><!-- .nav-links -->
        </nav><!-- #comment-nav-above -->
        <?php endif; ?>

    <ol class="ast-comment-list">
            <?php
            wp_list_comments(
                array(
                    'callback' => 'astra_theme_comment',
                    'style'    => 'ol',
                )
            );
            ?>
        </ol><!-- .ast-comment-list -->

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
        <nav id="comment-nav-below" class="navigation comment-navigation" aria-label="<?php esc_attr_e( 'Comments Navigation', 'astra' ); ?>">
            <h3 class="screen-reader-text"><?php echo esc_html( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></h3>
            <div class="nav-links">

                <div class="nav-previous"><?php previous_comments_link( astra_default_strings( 'string-comment-navigation-previous', false ) ); ?></div>
                <div class="nav-next"><?php next_comments_link( astra_default_strings( 'string-comment-navigation-next', false ) ); ?></div>

            </div><!-- .nav-links -->
        </nav><!-- #comment-nav-below -->
        <?php endif; ?>

    <?php endif; ?>

    <?php
        // If comments are closed and there are comments, let's leave a little note, shall we?
    if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
        ?>
        <p class="no-comments"><?php echo esc_html( astra_default_strings( 'string-comment-closed', false ) ); ?></p>
    <?php endif; ?>

    <?php comment_form(); ?>

    <?php astra_comments_after(); ?>

</div><!-- #comments -->

Upvotes: 0

Ricardo Gon&#231;alves
Ricardo Gon&#231;alves

Reputation: 5114

I always prefer to use a WordPress core function, if there is one to do what I want.

You can easily achieve that using get_comments. With the countarg set to true. This way it will retrieve only the comments count.

$args = array(
    'post_id' => $post->ID, //main post id
    'parent' => get_comment_ID(), //the comment id
    'count' => true, //just count
);
$comments = get_comments($args); //number of comments;

Upvotes: 2

Vladislav Sivachuk
Vladislav Sivachuk

Reputation: 52

why don't you write such a custom function to get subcomments for each individual comment by ID like this?

function subreplies_counter($id){
  global $wpdb;
  $query = "SELECT COUNT(comment_ID) AS count FROM $wpdb->comments 
  WHERE `comment_approved` = 1 AND `comment_parent` = $id";
  $data = $wpdb->get_row($query);
  return $data->count;
}

Upvotes: 0

Related Questions