Posts and Comments Together, Oh My!

A few days ago, Captain Primate asked:

WordPress doesn’t have something like this built-in I guess because of the different ways that posts and comments are managed and have metadata attached to them. But, if users are logged in when they comment (which you can require in the Settings -> Discussion page) then comments are affiliated with the accounts of anyone who is logged into your site when they comment. The trick is then to display that information.

I did something like this for a jumbo psychology course where the faculty member wanted to get quick summary information of the work students had contributed to the site. I had originally tied the process to the plugin WP-Stats because we were looking at lots of other data (WP-Stats allows you to generate all sorts of stuff… I think D’Arcy Norman originally turned me onto it). But when looking at it today I realized those are separable processes, and it’s really pretty straightforward. I’ve thrown together a child theme of Twenty Eleven that includes the following additions to the author.php file (wrap them in php open and close tags):

At line 29 I added this code which generates a numerical summary of posts and comments by the user whose author archive you’re visiting:

							global $wpdb;
							$user_id = $post->post_author;  //change this if not in a std post loop
							$where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;
							$comment_count = $wpdb->get_var(
    						"SELECT COUNT( * ) AS total
							FROM {$wpdb->comments}
							$user = get_userdata($user_id);
							$post_count = get_usernumposts($user->ID);
							echo '<p><h1 class="entry-title">Summary of Activity on this Site</h1></strong><br />Number of Posts: ' . $post_count .' <br/> Number of Comments: ' . $comment_count . '</p>';

At line 71 I added an echo to mark off the POSTS section.

And at line 84 I added this code which pulls and displays a list of comments by that author with a link to the post they commented upon:

				echo '<h1 class="entry-title"><em>Comments:</em></h1>';
				$comment_author = get_the_author_id();
				$comments = get_comments(array(user_id=>$comment_author));
				foreach($comments as $comment) :
				$url = '<a href="' . get_permalink($comment->comment_post_ID) . '">' . get_the_title($comment->comment_post_ID) .'</a>';
				echo ('"' . $comment->comment_content . '" <br><em> posted on ' . get_comment_date('M j, Y') . ', on the post ' . $url .  '</em><br /><br>');

It is helpful also to be able to generate a list of authors so you can navigate easily to their author archives… the plugin we were using to enable this widget for the past few years — Authors Widget (no longer in the repository) — was ferkakte, so for that function I started using a Samsarin PHP Widget with the following code in it:

function ListTheAuthors() 
function widget_ListAuthors($args) {
  echo $before_widget;
  echo $before_title;?>Site Authors<?php echo $after_title;
  echo $after_widget;

A couple months ago Jason Parkhill threw that code into a little plugin to make a very basic List Authors widget (thanks Jason!). You can play with the line ‘optioncount=1&orderby=name&show_fullname=true’ to get the configuration you want for your site. Use this as a guide to making modifications.

Here’s Jason’s List-Authors plugin.

And here’s the TwentyEleven child theme I threw together that displays posts and comments together on an author’s archive page (remember… authors must be logged in when they comment or their comments will not appear on their archive pages; and, of course, you must have Twenty Eleven in your wp-content/themes folder). It’s not elegant (if there are no comments, you just get a blank space below “Comments”), but, it gets the job done. Please hack it and share it to your heart’s content!

One thought on “Posts and Comments Together, Oh My!”

  1. This is awesome Luke, the List Authors plugin we were using on UMW Blogs was throwing errors. Love the work you are doing on WordPress, I need to dice back in a bit these days.

Comments are closed.