Forums

small problem with select query (4 posts)

  1. echstudios
    Member
    Posted 3 months ago #

    Below is a simple custom select query of wp_comments. $myposts is actually an array of post ids, but it is not working. Works fine with a single post id though.

    $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 1 AND user_id = $user AND comment_post_id = $myposts");

    My question is how do I get this to count just the comments from a specific user of only the post_id's in the array $myposts?

    Thanks!

  2. Shane G
    Member
    Posted 3 months ago #

    Hi,

    You can easily use this plugin in order to count the comment:

    http://wordpress.org/extend/plugins/comment-count/

    Thanks,

    Shane G,

  3. echstudios
    Member
    Posted 3 months ago #

    Thanks shane, but I'm really looking to do this without a plugin. What I'm doing is actually more complicated than my example and I really prefer having full control with my own functions.

    I really just need to know how to run an sql count query of a specific array of post ids?

    Thanks, again!

  4. apljdi
    Member
    Posted 3 months ago #

    You can't feed MySQL an array like that. Place $wpdb->show_errors(); just above your query and you'll see what is happening.

    What you can do is something like this:

    $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 1 AND user_id = $user AND comment_post_id IN (".implode(',',$myposts).")";

    Why count(*) instead of count('comment_ID'), for example?

Reply

You must log in to post.

About this Topic