Yet another WordPress code snippet ;) I’ve got two major categories in my upcoming project – Blogs and Arts. In the archive, somewhere in the sidebar, I’d like to output the recent comments only from the chosen category, excluding the ones that are from the other category. The most difficult part here is to figure out proportions, because running this hack recursively may eat up all your memory. For instance you have two categories and you’d like to output 10 comments for each. The posts in the first category get commented twice as often as the posts in the second category, so the proportions here would be approximately 2:1, but it also depends on how often “the switch” is.
For instance, taking 20 comments in a stream (one after the other, ordered by publish date), imagine the first 10 are from the first category and the last 10 are from the second. You’d have to query at least 20 comments to retrieve the needed amount. Here’s another example – take 90 comments in a stream, 60 are from the first, 30 are from the second. You need 10 comments for each, your ratio is 2:1 but you need to cycle at least 70 comments to make sure you display 10 from the second category. I know this is insane, but you’ll get my point when you’ll lack a few comments in your sidebar, especially if the widget is titled “10 recent comments” ;)
Here’s the code:
<?php $show_comments = 10; $i = 0; $comments = get_comments("number=50&status=approve"); foreach ($comments as $comment) { $comm_post_id = $comment->comment_post_ID; if (!in_category("blogs", $comm_post_id)) continue; $i++; // Output the comment, author and whatever you need // I'll just output the comment excerpt to keep my code simple ?><li><?php comment_excerpt(); ?></li><?php if ($i >= $real_comments) break; } ?>
So 50 is the number that you need to figure out. Don’t take too much if you don’t need to, otherwise you’ll just be getting too much rows from your database that you’ll never use and slow down your application. Run a few tests, use a logger for debugging and make sure you’re caching the results.
thanks. works great. not a php coder, so what is the correct code to return the author's name along with the title of the post the comment appeared on, with a link to the comment and below that the comment excerpt?
Hi Paul, take a look at this link, it explains it all: http://codex.wordpress.org/Function_Reference/get…
thank ……..
i want to display recent comment in my site .
it works
It's only showing one comment. I can't get it to show anymore than that. Not sure what I'm doing wrong…
Perhaps you could let me take a look at the code.. Use pastebin or something.
Nothing to show really. I just copy and pasted the code you provide and then changed "blogs" to my specific category, yet it only shows one response.
I got it working, but I had to change:
if ($i >= $real_comments) break;
to
if ($i >= $show_comments) break;
It's a little different, but good to go as long as it works ;)
I just used Big Skillet's solution and it works fine. My only problem now is to set up a condition so that if there are comments it'll show them, otherwise it'll show tel say, "no comments yet". I tried this:
But doesnt work. Any idea ?
Thanks a lot anyway for this piece of code, helped me a lot on a complicated wp dev.
Shit the pastebin didnt work. Maybe this:
http://pastebin.com/embed_js.php?i=ZgiqsQAX
Perhaps you should run the condition on $real_comments or $show_comments, but not $comments.
You might try this query, which should give you comments for a specific categories (replacing with the cat_IDs you're seeking comments from:
select wp_comments.*
from wp_posts inner join wp_comments on wp_comments.comment_post_ID = wp_posts.ID
LEFT JOIN wp_term_relationships ON (wp_comments.comment_post_ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms ON (wp_terms.term_id = wp_term_taxonomy.term_id)
where wp_terms.term_id in ()
order by comment_date desc
Interesting, though custom SQL queries is not a very good idea, you'll be better off by doing JOINs in the appropriate filter for WP_Query. P.S. when doing SQL you should always use the $wpdb object to specify tables, as their names (i.e. the prefix) could be different.
Sorry – I cant get it to display properly here without the post using the content and displaying it. Perhaps Konstantin can fix? Sorry about the multiple posts.
Razor, please use some pastebin to post your code ;)
Thank you for the wonderful post.
This is a different version with gravatar and list of comments from the category.
$yourcat->slug is the category slug (fetched dynamically)
here is the code http://pastebin.com/raw.php?i=4FcyrwtM
Hi
Thanks for a great plugin.
are there any way to link the comment to the post it from?
Would be great and im thankful for help.
Anna