• Resolved Lolley

    (@lolley)


    I love this theme! But now I have been asked if the posts on the front page can show in a random order rather than desc or asc.

    Please advise!

    Many thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Lolley

    (@lolley)

    There isn’t much response in this forum.

    Fortunately, I was able to work out the solution by going elsewhere.

    Add this to the functions file to randomise posts:

    //Randomise posts on front page
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts($query) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set('orderby', 'rand');
        }

    Hi Lolley,

    Thanks fot your post, was thinking about to do the same but unfortunately I can’t get it working.
    I’m fairly new to wp but is it important where to put it in the functions.php?

    This is my site: https://www.urbx.nl and the frontpage are the recent posts…

    Well finally after many attempts is works.
    Thanks again Lolley…

    Hi,

    I would like to the same as Lolley and I paste the code anywhere in the functions.php but that broke everything so I had to go back… Sorry I am completly new with this so could you please let me know exactly where to paste it or what to do ?
    Many thanks in advance

    I just was searching for the backticks in OSX and see Lolley answered allready ??

    Thread Starter Lolley

    (@lolley)

    I simply added it to the bottom of functions.php, just before the closing tag, like this:

    //Randomise posts on front page
    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts($query) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set('orderby', 'rand');
        }
    }
    
    ?>

    If you are using a child theme (the better way tbh) then create a new functions.php in the child’s directory and add the code between php tags.

    Now there is one thing I should point out! Since this calls random posts you can get repetition of posts between pages of posts. When I move to the next page of posts I can see one or two that were on the first page! It doesn’t matter that much, but just so you are aware!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can front page posts show randomly?’ is closed to new replies.