I am following through the "Professional Wordpress Plugin Development" book from WROX and I wanted to use this piece of code I found in it (I switched pre_get_posts with pre_get_comments) for my plugin, but for some reason it isn't working? And when I googled it, I found multiple people who said that "pre_get_comments" stopped working sometime this year. Is this true? Because I can't find anything about it on the wordpress codex. And what hook should I be using instead then?

do_action_ref_array( 'pre_get_comments', 'random_order_comments');
function random_order_comments( $query )
    if( is_single() ){
            $query->set( ‘orderby’, ‘rand’ );

1 Answer 1


I don't see any reason why pre_get_comments would not be working, it's still in core, however...

you've got a few problems in your code -

do_action_ref_array is what core calls to trigger the action, you add your own functions to the action queue via add_action.

orderby is the field name you want to order by, order controls how it's ordered, and ASC and DESC are the only valid values for order.

WP_Comment_Query returns an array of comments, you could use PHP's shuffle to randomize the order.

Also note that you have curly typographer quotes in your code above around orderby and rand, which will throw a PHP fatal error. You do have debugging enabled, yes?

  • I have it enabled now! And thank you for the respond.
    – Yorick89
    Commented Oct 21, 2015 at 0:38

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.