Title: Random order by default?
Last modified: March 19, 2020

---

# Random order by default?

 *  Resolved [Deon](https://wordpress.org/support/users/deon-b/)
 * (@deon-b)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/)
 * Hi,
    thank you for this amazing plugin. I added the shortcode to hundreds of 
   posts and I just realized the order is always the same. I read your documentation
   about adding `orderby="rand"` but now it’s too late, I would have to manually
   add it in hundreds of posts, after I have already spent days adding the short
   code manually to them.
 * Maybe there is a way to add a code in functions.php that by default displays 
   them in random order?

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

 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/#post-12566131)
 * Here’s a tutorial on customizing the default attributes: [https://displayposts.com/2019/01/04/change-default-attributes/](https://displayposts.com/2019/01/04/change-default-attributes/)
 * Thanks
 *  Thread Starter [Deon](https://wordpress.org/support/users/deon-b/)
 * (@deon-b)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/#post-12566209)
 * Hi,
    thanks, I did see that, and in fact I also used it on my website to set 
   25 as my default number of related posts.
 * But which section do I have to edit to make it random by default?
 *  Thread Starter [Deon](https://wordpress.org/support/users/deon-b/)
 * (@deon-b)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/#post-12566326)
 * The only example you give is to change the number of posts to 20. Nowhere it’s
   mentioned what to change to make it random by default.
 * > If you find yourself typing the same parameters over and over, you can save
   > time by changing the default attributes. These can always be overridden by 
   > specifying new attributes on an individual shortcode.
   > For instance, let’s say you always list 20 posts with excerpts:
    -  This reply was modified 6 years, 2 months ago by [Deon](https://wordpress.org/support/users/deon-b/).
 *  Plugin Author [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * (@billerickson)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/#post-12566483)
 * Any attributes you type into the shortcode could be added to that function as
   a default attribute. The tutorial provides a general example. I didn’t write 
   a detailed example for every one of the dozens of parameters in the shortcode.
 * For your specific usage, add the following code to your theme’s functions.php
   file, a [core functionality plugin](https://www.billerickson.net/core-functionality-plugin/),
   or the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin:
 *     ```
       <?php
   
       /**
        * Set Defaults in Display Posts Shortcode
        * @see https://displayposts.com/2019/01/04/change-default-attributes/
        *
        * @param array $out, the output array of shortcode attributes (after user-defined and defaults have been combined)
        * @param array $pairs, the supported attributes and their defaults
        * @param array $atts, the user defined shortcode attributes
        * @return array $out, modified output
        */
       function be_dps_defaults( $out, $pairs, $atts ) {
       	$new_defaults = array( 
       		'orderby' => 'rand',
       	);
   
       	foreach( $new_defaults as $name => $default ) {
       		if( array_key_exists( $name, $atts ) )
       			$out[$name] = $atts[$name];
       		else
       			$out[$name] = $default;
       	}
   
       	return $out;
       }
       add_filter( 'shortcode_atts_display-posts', 'be_dps_defaults', 10, 3 );
       ```
   

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

The topic ‘Random order by default?’ is closed to new replies.

 * ![](https://ps.w.org/display-posts-shortcode/assets/icon-256x256.jpg?rev=2940963)
 * [Display Posts - Easy lists, grids, navigation, and more](https://wordpress.org/plugins/display-posts-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/display-posts-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/display-posts-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/display-posts-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/display-posts-shortcode/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Bill Erickson](https://wordpress.org/support/users/billerickson/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/random-order-by-default/#post-12566483)
 * Status: resolved