Title: display activitys posts randomly
Last modified: August 1, 2023

---

# display activitys posts randomly

 *  [wmasat](https://wordpress.org/support/users/wmasat/)
 * (@wmasat)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/display-activitys-posts-randomly/)
 * Hallo
   I need help with a buddypress, I want to display activitys posts randomly
   on home page, I want posts to be displayed randomly, not in chronological orderanyone
   knows any code or method for that
    -  This topic was modified 2 years, 10 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fdisplay-activitys-posts-randomly%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Zack Krida](https://wordpress.org/support/users/zackkrida/)
 * (@zackkrida)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/display-activitys-posts-randomly/#post-16939667)
 * Hi, [@wmasat](https://wordpress.org/support/users/wmasat/)! I was going to direct
   you to BuddyPress support, but I see that you’ve [already posted there](https://buddypress.org/support/topic/display-activitys-posts-randomly-2/).
 * For some more general advice, WordPress has built-in support for randomizing 
   post order. Here’s some example code and references to code documentation to 
   get you started.
 *     ```wp-block-code
       <?php
           $args = array(
               'post_type'      => 'your_custom_post_type', // replace with your custom post type slug
               'posts_per_page' => X, // replace with the number of posts you want to display
               'orderby'        => 'rand'
           );
   
           $query = new WP_Query( $args );
   
           if ( $query->have_posts() ) {
               while ( $query->have_posts() ) {
                   $query->the_post();
   
                   // Template for displaying the post
                   // For example, you could display the post title like this:
                   echo '<h2>' . get_the_title() . '</h2>';
                   // Add more output as per your needs.
               }
           } else {
               // If no posts match this query, output this text.
               _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
           }
   
           wp_reset_postdata();
       ?>
       ```
   
    - WP_Query: This is the class used to create the custom query. ([https://developer.wordpress.org/reference/classes/wp_query/](https://developer.wordpress.org/reference/classes/wp_query/))
    - the_post(): This function sets up post data to be used in the WordPress Loop.(
      [https://developer.wordpress.org/reference/functions/the_post/](https://developer.wordpress.org/reference/functions/the_post/))
    - get_the_title(): This function returns the title of the current post in the
      loop. ([https://developer.wordpress.org/reference/functions/get_the_title/](https://developer.wordpress.org/reference/functions/get_the_title/))
    - have_posts(): This function checks if there are more posts available in the
      loop. ([https://developer.wordpress.org/reference/functions/have_posts/](https://developer.wordpress.org/reference/functions/have_posts/))
    - wp_reset_postdata(): This function ensures that the global $post variable 
      is restored back to the – current post in the main query. ([https://developer.wordpress.org/reference/functions/wp_reset_postdata/](https://developer.wordpress.org/reference/functions/wp_reset_postdata/))
 * Keep in mind that the ‘rand’ orderby parameter can be somewhat slow if you have
   a lot of posts.
    -  This reply was modified 2 years, 10 months ago by [Zack Krida](https://wordpress.org/support/users/zackkrida/).
      Reason: fix some formatting
 *  [Ian Sackofwits](https://wordpress.org/support/users/plantprogrammer/)
 * (@plantprogrammer)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/display-activitys-posts-randomly/#post-16939712)
 * Hey, wmasat, I’d recommend making a new support topic in the Buddypress support
   threads: [https://buddypress.org/support/](https://buddypress.org/support/)

Viewing 2 replies - 1 through 2 (of 2 total)

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [Ian Sackofwits](https://wordpress.org/support/users/plantprogrammer/)
 * Last activity: [2 years, 10 months ago](https://wordpress.org/support/topic/display-activitys-posts-randomly/#post-16939712)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
