• Hello!

    I have already read a lot on the subject, but somehow nothing really worked.

    I would like to display the blogs on my site in a random order. The order should change as soon as the website is reloaded. Would you guys have any ideas about this? That would be great!

    I would like to give you more info but don’t know what else you need….

    Greetings and thanks in advance
    Nicolas

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 17 total)
  • Try searching random post plugin.

    Maybe try this?
    https://www.wpbeginner.com/wp-tutorials/how-to-display-random-posts-in-wordpress/

    Or this?
    https://stackoverflow.com/questions/8672401/get-random-post-in-wordpress

    • This reply was modified 3 years, 10 months ago by Gustav.
    Thread Starter nicolasanderes

    (@nicolasanderes)

    Hey @4ever16

    Thanks for your reply, unfortunately neither worked as it either changed the template or only worked for a “border section”…. :/

    @nicolasanderes are those posts or pages and what theme do u use?

    Thread Starter nicolasanderes

    (@nicolasanderes)

    @4ever16 We use the Peggi child theme.

    These are individual posts that we display on one page/category for overview. (See the link above)

    This code worked for me. Place it in functions.php

    add_action('pre_get_posts','alter_query');
    function alter_query($query){
        if ($query->is_main_query() &&  is_home())
            $query->set('orderby', 'rand'); //Set the order to random
    }
    Thread Starter nicolasanderes

    (@nicolasanderes)

    Hey @4ever16

    Thanks a lot for your super help!
    Unfortunately I’m on the tube right now, I have inserted the code in functions.php, but it has not changed anything. I assume I have inserted it in the wrong place?

    Where exactly should I place it?

    Thank you very much!

    1. Are those posts or pages?

    2. Try to search for all functions.php and place code there.

    3. Go to settings click reading check latest post instead of a static page.

    • This reply was modified 3 years, 10 months ago by Gustav.

    Aha ok i know what the problem is wait i will answer soon.

    • This reply was modified 3 years, 10 months ago by Gustav.
    Thread Starter nicolasanderes

    (@nicolasanderes)

    @4ever16 you’re a hero! ??

    First code worked but the problem is that the link you want to change is:
    https://makerstars.org/category/mit-strom/
    Instead of
    https://makerstars.org/

    Try this.

    function wpse83754_filter_pre_get_posts( $query ) {
        if ( $query->is_main_query() && is_page( $id ) ) {
            $query->set( 'post_type', array( 'mit-strom' ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );

    Or try this.

    function wpse83754_filter_pre_get_posts( $query ) {
        if ( $query->is_main_query() && is_page( $id ) ) {
            $query->set( 'post_type', array( 'category/mit-strom' ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
    • This reply was modified 3 years, 10 months ago by Gustav.
    Thread Starter nicolasanderes

    (@nicolasanderes)

    Hey! @4ever16

    thanks a lot for your time, i really appreciate it…. I have now inserted the codes, but unfortunately it still does not want to work…. Do I have to insert them at a certain place in Functions.php? As far as I know not really?

    Well the problem is “/category/name”

    Well hope someone else can help you.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It’s entirely possible that the code works, but your caching plugin is not serving new HTML for each visit. Try disabling the batcache plugin. If that fixes it and random order is more important than speed, disable caching on your home page.

    Thread Starter nicolasanderes

    (@nicolasanderes)

    Thank you @sterndata for your answer!

    No, the website speed is more important to us after all, especially since it is already slow…. If this is the only way, we will do without it.
    Likewise, it would be important for us to keep the design and that rules out most plugins…. :/

    @nicolasanderes HTML code on websites generally so lite when compare with css and js.May you can disable cache for only HTML to test and if there is no important slowing, you can continue without HTML cache and random feature.It is only advice

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Random order blog posts’ is closed to new replies.