WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I make the comment count on a post on my index page include comments that have been made on, in my case, individual gallery pictures that are included in the post? It seems that comments made on attachments like this aren't counted using the standard WordPress comments functions.

share|improve this question

A gallery is a post with attached media. So maybe you'll find hints like this : just hook on get_comment_numbers() and pass $attachment_id.

I think in this case you'll have to add an SQL query to get the comment count for attachment and then you'll be able to add it to the total number of comments for post , something like this :

global $wpdb;
$wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE post_type = 'attachment'" ) ;

I haven't tested yet. Hopefully this will help you.

share|improve this answer
The images in a gallery might be attached to other posts. post_parent is not useful in this case. – toscho May 13 '13 at 19:36
right, I missed that. – JMau May 13 '13 at 19:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.