0

By default when we open any author's page, WordPress displays latest posts from that author first.

What I want: If anyone opens any author's page, it will display random posts from that author.

I have searched everywhere but I couldn't find any solution.

1 Answer 1

0

use this query in themes functions.php

function ng_author_query( $query ) {
    if ( $query->is_author()
        && $query->is_main_query() ) {
            // your code to set $current_user_name here
            $query->set( 'orderby', 'rand' );
    }
}
add_action( 'pre_get_posts', 'ng_author_query' );

Let me know if you have any problem?

4
  • yes, it works, thank you so much. you made my day :) Commented Aug 16, 2018 at 15:07
  • & i have one more question what should i have to do : To display random post, whenever someone opens any Tag page or any Category page Commented Aug 16, 2018 at 15:09
  • use same query change only check $query->is_tag() $query->is_category() Commented Aug 16, 2018 at 15:18
  • great, it's working, god bless you, thanks one again. Commented Aug 16, 2018 at 15:34

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.