WordPress.org

Ready to get started?Download WordPress

Forums

Parse the comment number for a plugin (4 posts)

  1. CounterDax
    Member
    Posted 8 years ago #

    Hi all,
    I'm quite a newby when it comes to creating plugins and the Plugin APIs and I'm having some trouble.

    I am trying to figure out a way to get the comment number of a certain post (thus within the loop) so I can use this number in a plugin. The hooks and filters do not seem to do the job, so all I've got now is that it keeps echoing the comment number instead of parsing it as a string for use within php.

    Anyone an idea?

  2. Kafkaesqui

    Posted 8 years ago #

    "The hooks and filters do not seem to do the job"

    What hooks and filters? What are you presently doing to try and collect the comment count?

  3. Just query the database...

    $numberofcomments = $wpdb->query("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '45' AND comment_approved = '1'");

  4. CounterDax
    Member
    Posted 8 years ago #

    Thanks, I was already working on that but it didn't work at all.
    I rewrote the script you gave me a bit to:

    global $wpdb, $post_id;
    $get_comments_number = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'";
    if (!$all_posts) $get_comments_number .= " AND comment_post_ID=$post_id";
    $comments = $wpdb->get_var($get_comments_number);

    Thanks for the help! Now, I've written my 3rd plugin (very simplistic ones) and maybe I'll get the hang of it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags