0

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.

2 Answers 2

1

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>';
    }
}
1
  • 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, 2012 at 19:13
1

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

3
  • I'm sorry, but where is comments_clauses located?
    – deathlock
    Oct 17, 2012 at 14:49
  • @deathlock Even Notepad++ has a cross file search implemented.
    – kaiser
    Oct 17, 2012 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, 2012 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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