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

I'm trying to display comments in the sidebar from only the current category. So far I've got this:

    // Posts per page setting
    $ppp = 8; //get_option('posts_per_page'); // either use the WordPress global Posts per page setting or set a custom one like $ppp = 10;
    $custom_offset = 0; // If you are dealing with your custom pagination, then you can calculate the value of this offset using a formula

    // category (can be a parent category)
    $cat = get_query_var('cat');
    $category_parent = $cat;

    // lets fetch sub categories of this category and build an array
    $categories = get_terms( 'category', array( 'child_of' => $category_parent, 'hide_empty' => false ) );
    $category_list =  array( $category_parent );

    foreach( $categories as $term ) {
        $category_list[] = (int) $term->term_id;

    // fetch posts in all those categories
    $posts = get_objects_in_term( $category_list, 'category' );

    $sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID, user_id, comment_author
     FROM {$wpdb->comments} WHERE
     comment_post_ID in (".implode(',', $posts).") AND comment_approved = 1
     ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset";

    $comments_list = $wpdb->get_results( $sql );

    if ( count( $comments_list ) > 0 ) {

        $date_format = get_option( 'date_format' );

        foreach ( $comments_list as $comment ) {

                <a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>#comment-<?php echo($comment->comment_ID); ?>"><?php echo get_avatar( $comment->user_id, 50 ); ?></a>
                <span><strong><?php echo($comment->comment_author); ?></strong> commented on</span>
                <h3><a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>"><?php echo get_the_title ( $comment->comment_post_ID ); ?></a></h3>
                <span><?php echo($comment->comment_date); ?></span>
                <p>"<?php comment_excerpt(); ?>" <a href="<?php echo ( get_permalink( $comment->comment_post_ID ) ); ?>#comment-<?php echo($comment->comment_ID); ?>">Read More</a></p>
                <div class="clearfloat"></div><!-- Very Important -->


    } else {
        echo '<p>No comments</p>';

This either displays 0 comments or all comments everywhere, so it doesn't work. I don't understand what I did wrong...

share|improve this question
What are "category specific comments"? Comments can be attached to a post type, not a taxonomy. – kaiser Jul 24 '12 at 12:51
@kaiser category specific means, get only comments from posts in certain category. – Sandro Dzneladze Jul 24 '12 at 13:07
Then please update your question to be more specific. Not everyone reads through code or comments, when thinking about an answer. – kaiser Jul 24 '12 at 13:32

Try this function:

function get_category_comments($category_id, $limit = 5) {

    global $wpdb;

    $sql = "
        SELECT {$wpdb->comments}.comment_ID
        WHERE 1=1
            AND {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID
            AND {$wpdb->term_relationships}.object_id = {$wpdb->posts}.ID
            AND {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id
            AND {$wpdb->comments}.comment_approved = '1'
            AND {$wpdb->term_taxonomy}.term_id = '{$category_id}'
        ORDER BY {$wpdb->comments}.comment_date DESC
        LIMIT 0, {$limit}
    $comments = array();
    foreach ($wpdb->get_results($sql) as $c)
        $comments[] = get_comment($c->comment_ID);
    return $comments;


Then you can have another function to display the comments:

function display_category_comments() {

    global $cat;
    if (!$category_id = intval($cat))
        return false;

    $comments = get_category_comments($category_id);
    <?php foreach ($comments as $c) : $post = get_post($i = $c->comment_post_ID); ?>
            <?php echo $c->comment_author; ?> - <a href="<?php echo get_permalink($post->ID); ?>" title="Permalink to <?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a>
    <?php endforeach; ?>
share|improve this answer

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.