BuddyDev

Search

[Resolved] display activitys posts randomly

Tagged: 

  • Participant
    Level: Initiated
    Posts: 1
    wmasat on #50082

    Hallo, I have a buddypress, I want to display activitys posts randomly on home page, I want posts to be displayed randomly, not in chronological order
    anyone knows any code or method for that
    I tried all the ways to do this but to no avail, please any help

  • Keymaster
    (BuddyDev Team)
    Posts: 24250
    Brajesh Singh on #50083

    Hi,
    Thank you for the question.
    I am assuming that you are referring to Sitewide activity stream and not the stream on user’s profile.

    This is doable but there is no good way to achieve it.

    I am sharing a proof of concept that will work but I highly discourage using it.

    
    /**
     * Make BuddyPress activity stream random.
     *
     * @author BuddyDev
     * This is a proof of concept that it is doable. We do not endorse doing it as it is error prone.
     */
    add_filter( 'bp_activity_paged_activities_sql', function ( $sql, $args ) {
    	// Let us find the limit clause.
    	$limits = explode( 'LIMIT', $sql );
    
    	// let us break the sql into two parts, one before order by and one after order by.
    	$query = explode( 'ORDER BY', $sql );
    
    	$sql = $query[0] . ' ORDER BY RAND() ';
    	if ( count( $limits ) == 2 ) {
    		$sql .= 'LIMIT ' . $limits[1];
    	}
    
    	return $sql;
    }, 10, 2 );
    
    

    Please put this code in your bp-custom.php and it will make sitewide BuddyPress activity random.

    Please let me know if it works for you or not?

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 1
    wmasat on #50087

    You are amazing, it works perfectly, thank you very much

  • Keymaster
    (BuddyDev Team)
    Posts: 24250
    Brajesh Singh on #50089

    You are welcome.
    It’s a pleasure to be of service!

    Regards
    Brajesh

The topic ‘ [Resolved] display activitys posts randomly’ is closed to new replies.

This topic is: resolved