admin管理员组

文章数量:1134246

I have a buddypress with a theme (youzify), I want to display activity posts randomly on home page, not in chronological order. Anyone knows any code or method for that?

I used this code but it doesn't work buddypress

function posts_random_order( $query ) {

    if ( $query->is_main_query() && $query->is_home() ) {

        $query->set( 'orderby', 'rand' );

    }

}

add_action( 'pre_get_posts', 'posts_random_order' );

I have a buddypress with a theme (youzify), I want to display activity posts randomly on home page, not in chronological order. Anyone knows any code or method for that?

I used this code but it doesn't work buddypress

function posts_random_order( $query ) {

    if ( $query->is_main_query() && $query->is_home() ) {

        $query->set( 'orderby', 'rand' );

    }

}

add_action( 'pre_get_posts', 'posts_random_order' );
Share Improve this question edited Aug 1, 2023 at 5:42 Arsalan Mithani 5534 silver badges15 bronze badges asked Jul 29, 2023 at 15:48 wmasatwmasat 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

And why not using simple wordpress code, changing args ?

$args = array(
        'post_type'      => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => '1',
    );
    $my_query = new WP_Query( $args );
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();

            /* do your post output here */

        } // end while
    } // end if

本文标签: pluginsRandomly display activity posts on home page with buddypress