Title: small problem with select query
Last modified: August 19, 2016

---

# small problem with select query

 *  [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/small-problem-with-select-query/)
 * 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!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [shane-g](https://wordpress.org/support/users/shane-g/)
 * (@shane-g)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/small-problem-with-select-query/#post-1182522)
 * Hi,
 * You can easily use this plugin in order to count the comment:
 * [http://wordpress.org/extend/plugins/comment-count/](http://wordpress.org/extend/plugins/comment-count/)
 * Thanks,
 * Shane G,
 *  Thread Starter [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/small-problem-with-select-query/#post-1182536)
 * 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!
 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/small-problem-with-select-query/#post-1182546)
 * 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?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘small problem with select query’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * Last activity: [16 years, 9 months ago](https://wordpress.org/support/topic/small-problem-with-select-query/#post-1182546)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
