Quick Tip: Re-order WordPress Comments

on Mar 03 in Wordpress by

I’m implementing a comment karma system (think Reddit on a website I’m building in WordPress). The strange thing is, WordPress already has a value for comment karma in the database, so there’s not much I need to “build” so much as “access”. With this system, I want to reorder the comments so those with the highest karma end up on top and the lowest on the bottom.

Initially, this doesn’t look very easy. The WordPress function for displaying comments, wp_list_comments(args), only has arguments to order the posts backward or forward by date. Nevertheless, what the WordPress documentation doesn’t tell you is that wp_list_comments also takes a second argument: an array of comment objects which defaults to $wp_query->comments. So, what we can do is reorder the comment list ourselves and pass that into the function like so: wp_list_comments(args, comments).

  1. global $wp_query;
  2. $comment_arr = $wp_query->comments;
  3. usort($comment_arr, 'comment_comparator');
  4. wp_list_comments($args, $comment_arr);
  5.  
  6. function comment_comparator($a, $b)
  7. {
  8. $compared = 0;
  9. if($a->comment_karma != $b->comment_karma)
  10. {
  11. $compared = $a->comment_karma < $b->comment_karma ? 1:-1;
  12. }
  13. return $compared;
  14. }

First, make sure $wp_query is a global variable, otherwise you could get an empty array. Pull the comments array out of the query, then sort it using PHP’s usort function with a custom comparator. Finally, call wp_list_comments with whatever arguments you’d normally pass and the now-sorted array of comments.

For the comparator function, what we have is two objects, and we need to return if the objects are equal (0), if $a is greater than $b (1) or if $b is greater than $a (-1). $a and $b are objects from the comment array (comment_arr), and we use the comment_karma properties to decide order.

That’s it! You’ve now reordered the comments based on the unused (by WordPress) comment_karma value. By changing what is compared, you can reorder the comments in any way you want. For instance, to order them by alphabetically by author name, you could compare $a->comment_author and $b->comment_author using PHP’s strcmp function.

  1. function comment_comparator($a, $b)
  2. {
  3. return strcmp($b->comment_author, $a->comment_author);
  4. }

Since strcmp is already a comparator function, it will return the values we want (-1, 0, 1), we just have to pass it the correct properties.

Notice we change the order of $a and $b. In all of these functions, certain interactions could give you backward results depending on how you want things ordered and your reverse_top_level argument for wp_list_comments. If you get a backward result, simply change the order of the arguments (when using strcmp) or swap -1 and 1 in your if statement (when using numerical values like in the comment karma example).

Tags: , , , ,

Slowing Down

on Dec 09 in Meta by

Posting frequency will be very diminished in the next several weeks as I work on the many projects I have.

Tags:

OpenCart vs. Magento

on Dec 02 in Critiques by

Recently, I’ve been playing around with two open source shopping cart CMS’s: OpenCart and Magento. I’d like to give my impressions of them. Read More…

Tags: , , , , , ,

To Enhance Productivity, Do Something Else

on Nov 11 in Productivity by

Have you ever stared a screen, hoping something would come to your head, waiting for it, waiting for it, waiting for it? “I just need to make this image,” “I just need to write this posts,” or “I just need to code this app.”

Well, stop it. That’s a terrible plan. Staring at the screen “thinking” is the worst way to think. Typically, starting the whatever, even if it’s in a joking, doodly way, will get your brain flowing in the right direction.

But if that doesn’t work or isn’t possible, just do something else. Read More…

Tags: , , ,

NaNoWriMo is Upon Us

on Oct 28 in Meta by

[The return of the quick plug: if you want to support National Novel Writing Month, writing, teaching children/young adults to write, and other awesome writing-related things, you can sponsor me for NaNoWriMo. All donated money goes to the Office of Letters and Light, the non-profit behind NaNoWriMo and other awesomeness.]

As previously stated, next month (November) is National Novel Writing Month. In the spirit of things, we will focus on writing, writing productivity, writing creativity, and more writing during November.

Looking forward to seeing you there!

Tags: ,