0

It is necessary to display comments on the page randomly using a shortcode. Now I output comments using a shortcode using the code:

add_action('wp_head', function (){
    global $postPage, $post;
    $postPage = $post;
});

add_shortcode('comments_block',function($attr, $content){
    global $post,$postPage;
    $savePost = $post;
    $post = $postPage;
    ob_start();
    comments_template();
    $output = ob_get_contents();
    ob_end_clean();
    $post = $savePost;
    return $output;
});

This displays the comments and below them a form for adding comments - everything is fine. But comments need to be displayed randomly (now they are standard by date).

I also tried to combine this code with the one presented here How can I show comments in random order? , but I don't have enough knowledge in this regard(

I will be grateful for any help in this regard.

1 Answer 1

0

Easy thing would be to override comment query and set orderby property to rand.

function wpse381090_comment_query_rand( $args ) {
    $args['orderby'] = 'rand';
    return $args;
}
add_filter('comments_template_query_args', 'wpse381090_comment_query_rand');
1
  • Thank you for your help! I added this to functions.php, but unfortunately did not help( Commented Jan 8, 2021 at 5:45

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.