0

I can't find a snippet to show my comments in random order (not the posts, there's tons of entries for that one). I need this for my community page - I don't want them ordered by date. It's part of the concept of my site, that the comments are equally important and therefore should be shuffled before beeing displayed.

Could anyone provide me with a snippet for my functions.php and maybe even an explenation on how it works (I'm obviously not that good yet, but I might learn something :)

EDIT SINCE I FAIL HORRIBLY, here's my unedited comment.php:

<?php
/**
 * The template for displaying comments.
 *
 * This is the template that displays the area of the page that contains both the current comments and the comment form.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy
 */

if (post_password_required()) {
    return;
}

//Here?
foreach ( $comments as $comment ) :
    echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;

?>

<div id="comments" class="uk-margin-large-top">

    <?php if (have_comments()) : ?>

        <h2 class="uk-h3 uk-heading-bullet uk-margin-medium-bottom"><?php printf(_n(__('Comment'), __('Comments (%s)'), get_comments_number()), number_format_i18n(get_comments_number())) ?></h2>

        <ul class="uk-comment-list" 
    //optional arguments in this array, without arguments will return all comments
    $args = array();
    $comments = get_comments( $args );
    // use shuffle to randomize the order of the comments 
    shuffle($comments);>
    <?php wp_list_comments([
                'style' => 'ul',
                'short_ping' => true,
                'callback' => 'comment_callback',
            ]) ?>
        </ul>

        <?php if (get_comment_pages_count() > 1 && get_option('page_comments')) : ?>
        <ul class="uk-pagination uk-flex-between">
            <li class="nav-previous"><?php previous_comments_link('<span uk-pagination-previous></span> ' . __('Older Comments')) ?></li>
            <li class="nav-next"><?php next_comments_link(__('Newer Comments') . ' <span uk-pagination-next></span>') ?></li>
        </ul>
        <?php endif ?>

    <?php endif ?>

    <?php if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) : ?>
        <p class="uk-margin-medium uk-text-danger"><?php _e('Comments are closed.') ?></p>
    <?php endif ?>

    <?php get_template_part('commentform') ?>

</div>

<?php

/**
 * The template to display a comment.
 */
function comment_callback($comment, $args, $depth) {
?>
<li id="comment-<?php comment_ID() ?>">
    <article id="comment-article-<?php comment_ID() ?>" <?php comment_class('uk-comment uk-visible-toggle') ?> tabindex="-1">
        <header class="uk-comment-header uk-position-relative">
            <div class="uk-grid-medium uk-flex-middle" uk-grid>
                <?php if ($args['avatar_size'] != 0) : ?>
                <div class="uk-width-auto">
                    <?= get_avatar($comment, $args['avatar_size']) ?>
                </div>
                <?php endif ?>
                <div class="uk-width-expand">
                    <h3 class="uk-comment-title uk-margin-remove"><?php comment_author_link($comment) ?></h3>
                    <p class="uk-comment-meta uk-margin-remove-top">
                        <a class="uk-link-reset" href="<?= esc_url(get_comment_link($comment, $args)) ?>">
                            <time datetime="<?php comment_time('c') ?>"><?php printf(__('%1$s at %2$s'), get_comment_date('', $comment), get_comment_time())
                        ?></time>
                        </a>
                    </p>
                </div>
            </div>
            <div class="uk-position-top-right uk-hidden-hover">
                <?php comment_reply_link(array_merge($args, [
                    'depth'     => $depth,
                    'max_depth' => $args['max_depth'],
                    'add_below' => 'comment-article',
                ])) ?>
            </div>
        </header>

        <div class="uk-comment-body">

            <?php if ($comment->comment_approved == '0') : ?>
            <p><?php _e('Your comment is awaiting moderation.') ?></p>
            <?php endif ?>

            <?php comment_text() ?>

            <?php edit_comment_link(__('Edit')) ?>

        </div>

    </article>
<?php
}

1 Answer 1

1

Never actually had a need for this but you will need to use the WP_Comment_Query function. There is this handy comments query generator online to customize this query easily.

But wait, it doesn't allow you to select a random order. No worries just add or replace the orderby parameter:

'orderby' => 'rand'

This should return comments ordered randomly. Let me know if you need any extra tips.

Edit: the above will not work.

I should have investigated deeper into the subject. The rand argument is not accepted in a comment query, as I thought it would, it is only accepted in post queries.

So how can you achieve this behavior. You can do it in an even simpler way using a built in PHP function to randomize arrays.

You should edit the theme's comment template, usually it is comments.php and replace the wp_list_comments() function with something like:

//optional arguments in this array, without arguments will return all comments
$args = array();
$comments = get_comments( $args );
// use shuffle to randomize the order of the comments 
shuffle($comments);

Now you need to iterate the $comments array, and do something like:

foreach ( $comments as $comment ) :
    echo $comment->comment_author . '<br />' . $comment->comment_content;
endforeach;

You can find more info on the get_comments() documentation page and the accepted arguments to customize the query.


8
  • Thanks for the quick reply mate! Sadly you've already lost me at 'handy comments query generator'. I tried to figure it out, but there's to many words I don't understand :) I have tried to edit the class-wp-comment-query.php file by putting the 'rand' argument in different places that seemed appropriate -> no luck so far. But that might give you an idea of my skill-level. Any extra tips?
    – Beni
    Apr 19, 2020 at 11:00
  • Well, to clarify - I'm no complete idiot. :) I know CSS and HTML, my wordpress runs on a child theme and I have FTP access to edit the files.
    – Beni
    Apr 19, 2020 at 11:05
  • sorry for the misleading information, I edited the original reply with a working solution.
    – Patareco
    Apr 19, 2020 at 14:00
  • you forgot to add the link
    – Patareco
    Apr 19, 2020 at 14:35
  • oh my god, you are quick .. I copied the code in my initial question due to character limitation.
    – Beni
    Apr 19, 2020 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.