BuddyDev

Search

[Resolved] Randomise Posts

  • Participant
    Level: Enlightened
    Posts: 40
    Rudder on #7278

    Hello Brajesh,

    Is it possible to randomise the posts using BuddyBlog?

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #7283

    Hi Ben,
    Thank you for posting.
    Since BuddyBlog utilises WP_Query for listing posts, you can use the orderby=rand for randomizing the posts list.

    Please see all the orderby options here

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 40
    Rudder on #7297

    Thank you Brajesh,
    I will add that now 🙂

  • Participant
    Level: Enlightened
    Posts: 40
    Rudder on #7300

    Hi brajesh,

    I created a function for use in the bp-custom and it works well,
    Thank you for the input 🙂
    I’ve added the code below just incease someone else wants to do the same.
    Ben


    add_action('pre_get_posts','randomize_query');
    function randomize_query($query){
    if ($query->set('orderby', 'rand')); //Set the order to random
    }

  • Participant
    Level: Enlightened
    Posts: 40
    Rudder on #7302

    EDIT:
    I forgot to make sure it knew it’s for posts, The way I had it then pages were randomised to, very annoying when you’re trying to find a page lol

    add_action('pre_get_posts','randomize_query');
    function randomize_query($query){
    if ($query->have_posts()) //Check that you have posts and now knows it's for posts
    $query->set('orderby', 'rand'); //Set the order to random
    }

  • Keymaster
    (BuddyDev Team)
    Posts: 24636
    Brajesh Singh on #7303

    Thank you Ben.
    You already realized it. I will also suggest completely avoiding pre_get_posts as it will have side effect.

    It will better to directly modify the template in this case. If you still want to use pre_get_posts, I suggest pairing it with the following conditions

    
    
    if ( function_exists( 'bp_is_buddyblog_component' ) && bp_is_buddyblog_component() ) {
    
    }
    
    

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 40
    Rudder on #7304

    Thank you Brajesh, That’s a much better way

You must be logged in to reply to this topic.

This topic is: resolved