Luciche
Luciche

Reputation: 63

Display wordpress comments in random order

How can I display the comments on wordpress posts/pages in a random order? Right now the only option is to have them displayed by new or old.

Basically on each page load, the comments should be shuffled so they appear in a random order

Upvotes: 0

Views: 254

Answers (2)

James Paterson
James Paterson

Reputation: 2915

You can add a filter onto the comments array - Putting this in your functions.php file should do the trick.

function shuffle_comments( $comments , $post_id ) {
    return shuffle( $comments );
}

add_filter( 'comments_array' , 'shuffle_comments', 10, 2);

If you don't know much about filters, essentially you can add them to parts of Wordpress to change data before it's displayed. This example is actually referenced in the comments_array filter reference.

Upvotes: 1

SpacePhoenix
SpacePhoenix

Reputation: 605

When the array of comments/posts has been grabbed, use PHP's Shuffle function https://www.php.net/manual/en/function.shuffle.php to shuffle the order around before the comments/posts are displayed. Each page load the comments/posts will be shuffled

Upvotes: 0

Related Questions