• Anonymous User 18405611

    (@anonymized-18405611)


    Assalamu alaikum (peace be upon you)

    I have Q&A Website on wp … On a question or blog posts, wp counting replies as parent comment.. I have 1 comment and 1 reply on a post, wp showing 2 comments on this post! Is there any way to count only parent comments across the entire site?

    • This topic was modified 3 years, 4 months ago by Anonymous User 18405611.
    • This topic was modified 3 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • There’s no built-in way that I can find, but it’s easy enough to do yourself witha little bit of custom code (note.. this is completely untested):

    $post_id = 5; // Make this whatever you need to for your own post ID.
    
    global $wpdb;
    
    $query = "SELECT
        COUNT(comment_ID)
        FROM ".$wpdb->comments."
        WHERE comment_parent = 0
            AND comment_post_ID = ".$post_id.";";
    
    $num_comments = $wpdb->get_var ($query);

    That will get the top-level comment count for a single post. If you want to get the count for all posts you can remove the last line of the query.

    Thread Starter Anonymous User 18405611

    (@anonymized-18405611)

    Where do I put the code snippet ?

    It depends on where you want the comment count to appear. In a shortcode would make sense if you want to display it in a page’s content. Here’s an example that uses the code provided above. Place this in your functions.php file or use the Code Snippets plugin.

    add_shortcode('comment_count', 'wpsf_comment_count_shortcode');
    function wpsf_comment_count_shortcode()
    {
      global $wpdb;
      $query = "SELECT
        COUNT(comment_ID)
        FROM " . $wpdb->comments . "
        WHERE comment_parent = 0";
    
      return $wpdb->get_var($query);
    }

    Then, you would insert the shortcode in the editor like this: “We have [comment_count] answers on the site.”

    Thread Starter Anonymous User 18405611

    (@anonymized-18405611)

    Thank you so much !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Replies counting as comments..’ is closed to new replies.