WordPress already provides an easy function for fetching the comments get_comments()
but the function only supports fetching comments of a particular user,
or for a particular post and not fetching comments of a particular
category. The code for the former two cases, is pretty simple but the
last one is not so simple. Here are the code snippets as examples:
Fetching Comments of a particular user
1 | $args = array ( 'number' => 10, 'status' => 'approve' , 'user_id' => 1 ); |
2 | $comments_list_by_user = get_comments( $args ); |
3 | print_r ( $comments_list_by_user ); |
Fetching Comments of a particular post
1 | $args = array ( 'number' => 10, 'status' => 'approve' , 'post_id' => 30 ); |
2 | $comments_list_post = get_comments( $args ); |
3 | print_r ( $comments_list_post ); |
Fetching Comments of a particular category
Here is how you can fetch comments for a specific category:
02 | $ppp = get_option( 'posts_per_page' ); |
09 | $categories = get_terms( 'category' , array ( 'child_of' => $category_parent , 'hide_empty' => false ) ); |
10 | $category_list = array ( $category_parent ); |
11 | foreach ( $categories as $term ) { |
12 | $category_list [] = (int) $term ->term_id; |
16 | $posts = get_objects_in_term( $category_list , 'category' ); |
18 | $sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID |
19 | FROM { $wpdb ->comments} WHERE |
20 | comment_post_ID in ( ".implode(',', $posts)." ) AND comment_approved = 1 |
21 | ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset "; |
23 | $comments_list = $wpdb ->get_results( $sql ); |
25 | if ( count ( $comments_list ) > 0 ) { |
26 | $date_format = get_option( 'date_format' ); |
28 | foreach ( $comments_list as $comment ) { |
29 | echo '<li>Comment: ' . substr ( $comment ->comment_content, 0, 50 ). '..<br />' . date ( $date_format , strtotime ( $comment ->comment_date ) ). '<br />Post: <a href="' .get_permalink( $comment ->comment_post_ID ). '">' .get_the_title( $comment ->comment_post_ID ). '</a></li>' ; |
33 | echo '<p>No comments</p>' ; |
What we have done here is that, first we fetch sub categories of the
category specified so as to include comments on those posts too which
are under a sub-category of the specified category. Then we build an
array of all these categories, where we need to look up for comments.
Then we fetch the posts ID under those categories and then we use a
query to fetch the comments on those posts. Finally, we display them as
per our need by iterating $comments_list.
In case you want to fetch the comments of a particular user under a
specific category, then you can just change the SQL query in the above
code to this:
2 | $sql = "SELECT comment_ID, comment_date, comment_content, comment_post_ID |
3 | FROM { $wpdb ->comments} WHERE |
4 | comment_post_ID in ( ".implode(',', $posts)." ) AND comment_approved = 1 AND user_id = $user_id |
5 | ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset "; |
I have used simple names for variables here to make it easy to
understand. Always take care to provide your code in its own namespace,
choose unique (non-generic) names. Read
prefix everything in WordPress.
Need help? Got questions? Comment section is all yours!
No comments:
Post a Comment