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).
$comment_arr = $wp_query->comments;
function comment_comparator($a, $b)
$compared = 0;
if($a->comment_karma != $b->comment_karma)
$compared = $a->comment_karma < $b->comment_karma ? 1:-1;
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.
function comment_comparator($a, $b)
return strcmp($b->comment_author, $a->comment_author);
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: array, comments, php, sorting, wordpress
Posting frequency will be very diminished in the next several weeks as I work on the many projects I have.Tags: meta
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, productivity, tips, writing
[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: meta, NaNoWriMo