WordPress + Paged Comments Plugin + Recent Comments in the Sidebar = Frustration
A recent upgrade of a project to WordPress 2.3.1 brought about a few other new upgrades. It’s one of those things where you figure, “while we’re in here we might as well fix up a few things.” So among the upgrades being made was the addition of the Paged Comments plugin on the blog. A small bit of styling and we were good to go with paging. No real issues to speak of until we tested the recent comments we were pulling into the sidebar on the site.
Prior to any of the upgrades there was a box in the right sidebar of the site that pulled in five recent comments courtesy of the Simple Recent Comments plugin. No issues to speak of with this plugin. Recent comments pulled in reliably.
So back to where we combined the paging comments with the sidebar. The recent comments plugin will pull the 5 most recent comments along with the permalink to that particular comment, such as:
http://cnpstudio.com/blog/2007/12/04/new-site-girl-scouts-of-west-
central-florida/#comment-97
But then what happens to that permalink once I add paging? Well, any comments on the default page of the post would still work
http://cnpstudio.com/blog/2007/12/04/new-site-girl-scouts-of-west-
central-florida/
But what if that comment is now on page 2 of the comments? Simple recent comments has no way of knowing this. Hence we start getting broken permalinks to comments because the actual permalink may now need to be:
http://cnpstudio.com/blog/2007/12/04/new-site-girl-scouts-of-west-
central-florida/comment-page-2/#comment-97
Granted, the simple recent comments and paged comments plugins were made by two separate developers and never intended to interface together. However, it’s not unreasonable to think that the Paged Comments plugin would come with a way to dynamically derive comment permalinks from pages other than the actual post page. Currently, the plugin only includes the logic to dynamically create the permalink on the actual comments page, but nowhere else. Frustrating.
That left us to do some custom work on our end to dynamically derive the permalinks in the sidebar. It wasn’t our ideal solution. In a perfect world, this would have already been addressed somewhere in the WordPress community, but that didn’t seem to be the case. Should we have used a different plugin altogether? Was the solution available on this plugin and we just went about this the hard way? With the amount of time we dedicated to solving the problem I don’t think so, but anything is possible.
Our solution fixed our one instance, but it was not a solution for all situations and therefore not something we’d release to the entire community. Anyone heard of a pre-existing solution to this before we spend the time creating it?



Add Your Comment3 Responses to “WordPress + Paged Comments Plugin + Recent Comments in the Sidebar = Frustration”
Tom on December 22nd, 2007 at 9:01 am
I have been DESPERATE to find a solution to this problem and have been trying to look for a patch, with no luck.
What code needs to be changed in this plugin?
nick on December 22nd, 2007 at 10:56 am
Tom,
First you should know our temporary fix was not in the slightest bit elegant, but it did satisfy an immediate need. We are developing a permanent fix that makes better use of resources, but until that’s ready here’s what you can edit within the Simple Recent Comments plugin to make this work.
Find the following foreach…
foreach ($comments as $comment)Add this SQL query at the beginning…
$sql = "SELECT count(*) AS total_comments FROM $wpdb->comments WHERE $wpdb->comments.comment_post_id = $comment->comment_post_ID AND comment_approved = '1' AND $wpdb->comments.comment_date_gmt < = '".$comment->comment_date_gmt."'";$total = $wpdb->get_results($sql);
This will return what number comment this is on the post (since WordPress does not actually store this number), so if this was the 23rd comment on a particular post then 23 is returned.
Now you need to figure out what page this falls on which is where the next piece comes in. Add this line next.
$page_num = ((int)($total[0]->total_comments / 10)) + 1;In the above, 10 is the number of comments to show per page (a number that you actually set in the options for this plugin in the WordPress admin). You will need to make this whatever your number of comments per page is. Like I said, not elegant. If this was perfect I would be giving you code that pulled this number from the options b/c now this becomes a possible problem if you ever change the number in the options but forget to update it here.
Now, in the $output string you will need to update the link that is generated…
get_permalink($comment->ID) . "comment-page-" . $page_num . "/#comment-Everything else can stay the same.
A few final thoughts… The fact that this queries the SQL db for each comment printing out with the plugin is my biggest gripe with this “fix”. This is mitigated by the fact that we’re caching the output, so as a result the strain on the server isn’t all that significant, but we live to keep things clean and this… well… isn’t. So, if you’re not caching output and you are driving a lot of traffic this can get messy. The final version we release, however, will.
mike on January 2nd, 2008 at 6:31 pm
After spending a little time to refresh my MySQL optimization skills (I’ve spent too much time in SQL Server lately), we were able to come up with a single query solution.
Just replace the original query with
$sql = "SELECT C.*, COUNT(C2.comment_ID) as comment_num FROM (SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,
SUBSTRING(comment_content,1,$src_length) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND post_status = 'publish'
ORDER BY comment_date_gmt DESC
LIMIT $src_count) C
LEFT JOIN $wpdb->comments C2 ON C2.comment_post_ID = C.comment_post_ID AND C2.comment_ID < = C.comment_ID AND C2.comment_approved = '1' AND C2.comment_type=''
GROUP BY C.comment_ID ORDER BY comment_date_gmt DESC";
Then use this line within the "foreach($comments as $comment)" loop
$page_num = ((int)($comment->comment_num / 50)) + 1;