Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been trying to create a sidebar widget to display recent comments from specific category. I'm having difficulties in determining how Wordpress knows from which categories the comments are.

I have checked the codex on get_comments function but it doesn't seem to allow fetching from specific categories. I've found some hints from Kovshenin's blog, but can't seem to make it work properly, because my try either shows all comments (disregarding the categories) or doesn't show it at all.

I'm basing the widget from Widget_Recent_Comments (found from wp-includes/default-widgets.php). Here is what I have so far. I believe the output is controlled around this area:

    $output = '';
    $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );

    if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
        $number = 5;

    $category_name = empty( $instance['category_name'] ) ? '' : $instance['category_name'];

    $comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );
    $output .= $before_widget;
    if ( $title )
        $output .= $before_title . $title . $after_title;

    $output .= '<ul id="recentcomments">';
    if ( $comments ) {
        foreach ( (array) $comments as $comment) {
            $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
        }
    }
    $output .= '</ul>';
    $output .= $after_widget;

    echo $output;

And here is the full codes: http://pastebin.com/VH666jty

Basically my widgets is similar to the default Recent Comments widget. Only that it has an optional Display comments only from category/categories: ..., where user could input by themselves.

Leaving it blank will display comments from all category, while putting some category slug, separated by comma (e.g.: mycategory,othercategory) will display comments only from those selected categories.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You could get your answer from Kovshenin blog

Replace whole if ( $comments ) with this

if ( $comments ) {
    foreach ( (array) $comments as $comment) {
    $comm_post_id = $comment->comment_post_ID;
    if ( $category_name ) {
        if (!in_category( "{$category_name}", $comm_post_id )) {
            continue;
        }
    }
    $output .=  '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
    }
}
share|improve this answer
    
Both answers seem to work, but I guess I'd choose this as it's easier for me. Thanks a lot, @MridulAggarwal and @Dipa! :) –  deathlock Oct 17 '12 at 19:13

You can use the comments_clauses hook to customize the comment query

Replace the "join" part with

INNER JOIN $wpdb->posts ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id)
INNER JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)

In the "where" part append the clause based on the user input for eg. AND $wpdb->terms.slug = 'uncategorized'

Make an add_filter call just before the get_comments function & use the remove_filter call immediately afterwards so as to not modify any other queries on the page

share|improve this answer
    
I'm sorry, but where is comments_clauses located? –  deathlock Oct 17 '12 at 14:49
    
@deathlock Even Notepad++ has a cross file search implemented. –  kaiser Oct 17 '12 at 15:15
1  
@kaiser yes, I'm sorry! :s I only tried Google when I commented, when I realized I should've search within my files. :s –  deathlock Oct 17 '12 at 15:18

Your Answer

 
discard

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.