Title: Random sort order
Last modified: August 20, 2016

---

# Random sort order

 *  Resolved [KS](https://wordpress.org/support/users/karl19/)
 * (@karl19)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/random-sort-order/)
 * Perhaps this isn’t a very common request, but I’m looking for a way to randomise
   images.
 * We’re using the plugin for a frontpage slideshow, to be able to create a list
   of images from many different pages – with Attachments, it’s easy to pick from
   the media library and make a custom slideshow. But we’re trying to randomise 
   it..
 * Ideally, I’d like to find a way to randomise the images, then keep the randomised
   list in a string, so that I can output the same list in a couple of places.
 * [http://wordpress.org/extend/plugins/attachments/](http://wordpress.org/extend/plugins/attachments/)

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

 *  Plugin Author [Jon Christopher](https://wordpress.org/support/users/jchristopher/)
 * (@jchristopher)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505802)
 * That’s a feature you’ll need to build by hand and won’t be part of Attachments’
   code base, but given the utility functions available you are able to implement
   what you’re looking for, it’s a matter of randomizing an array of numbers ranging
   from 0 to your total attachments, and then grabbing each one by one, using the
   utility functions offered. I’d love to help more but perhaps you have a friend
   who knows PHP and can help out?
 *  Plugin Author [Jon Christopher](https://wordpress.org/support/users/jchristopher/)
 * (@jchristopher)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505803)
 * Actually, you know what. This might in fact be within the scope of what could
   be expected. I’m going to think about what it would take and see if I can add
   it. In the meantime you can surely try something like I mentioned above and give
   it a shot by hand as well.
 *  Thread Starter [KS](https://wordpress.org/support/users/karl19/)
 * (@karl19)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505886)
 * Many thanks for your input Jonathan! Perhaps it would be a sensible option to
   be able to randomise the results – but you know best what features you think 
   are sensible for the plugin.
 * I didn’t manage to solve the randomising with Attachments and ended up using 
   a setup where I added categories to image attachments, then running a normal 
   WP query and randomising the results. Was the simplest way to finish this project.
   But I’m sure I’ll end up using Attachments again for another project in the future!
 *  Plugin Author [Jon Christopher](https://wordpress.org/support/users/jchristopher/)
 * (@jchristopher)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505950)
 * This has been implemented as of version 3.4, and [a usage example is provided in the docs](https://github.com/jchristopher/attachments#get-attachments).
   Enjoy!
 *  Thread Starter [KS](https://wordpress.org/support/users/karl19/)
 * (@karl19)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505951)
 * That’s great news! I’m sure it’ll come in very handy for a future project.
 * Thanks a lot for considering and implementing this feature! I’ve sent you a donation.
 *  Plugin Author [Jon Christopher](https://wordpress.org/support/users/jchristopher/)
 * (@jchristopher)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505952)
 * Wow! That’s super generous of you thank you so much, I hope you enjoy Attachments!
 *  [mttd](https://wordpress.org/support/users/mttd/)
 * (@mttd)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505992)
 * hi,
 * I can’t seem to get the randomise feature to work, even if i add in a shuffle(
   $attachments ) in to the code in the loop…
 * I’ve added the randomise function in to my functions.php file as directed but
   this breaks the script.
 * I’m just using the default instance name of ‘attachments’. any clues as to what
   is going wrong or what im doing wrong please?
 * any help much appreciated! i’m loving attachments as it’s just what i need, would
   be perfect if i could get this feature to work!
 * cheers,
    Tom
 *  Plugin Author [Jon Christopher](https://wordpress.org/support/users/jchristopher/)
 * (@jchristopher)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505993)
 * You’ll need to paste code samples and indicate which files they’re in. If your
   instance name is `attachments` the code you’d add to `functions.php` is
 *     ```
       function my_attachments_randomize( $attachments )
       {
           return shuffle( $attachments );
       }
   
       add_filter( 'attachments_get_attachments', 'my_attachments_randomize' );
       ```
   
 *  [mttd](https://wordpress.org/support/users/mttd/)
 * (@mttd)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505994)
 * below is what I have in my functions.php file (overriding the default instance
   as my pages were already setup using the default instance.
 *     ```
       function my_attachments_randomise( $attachments )
       {
           return shuffle( $attachments );
       }
   
       add_filter( 'attachments_get_attachments', 'my_attachments_randomise' );
   
       function attachments( $attachments )
       {
         $fields         = array(
           array(
             'name'      => 'title',                         // unique field name
             'type'      => 'text',                          // registered field type
             'label'     => __( 'Title', 'attachments' ),    // label to display
             'default'   => 'title',                         // default value upon selection
           ),
           array(
             'name'      => 'caption',                       // unique field name
             'type'      => 'textarea',                      // registered field type
             'label'     => __( 'Caption', 'attachments' ),  // label to display
             'default'   => 'caption',                       // default value upon selection
           ),
         );
   
         $args = array(
   
           // title of the meta box (string)
           'label'         => 'Page/Post Attachments',
   
           // all post types to utilize (string|array)
           'post_type'     => array( 'post', 'page' ),
   
           // meta box position (string) (normal, side or advanced)
           'position'      => 'normal',
   
           // meta box priority (string) (high, default, low, core)
           'priority'      => 'high',
   
           // allowed file type(s) (array) (image|video|text|audio|application)
           'filetype'      => array( 'image' ),  // no filetype limit
   
           // include a note within the meta box (string)
           'note'          => 'Attach files here!',
   
           // by default new Attachments will be appended to the list
           // but you can have then prepend if you set this to false
           'append'        => true,
   
           // text for 'Attach' button in meta box (string)
           'button_text'   => __( 'Attach Files', 'attachments' ),
   
           // text for modal 'Attach' button (string)
           'modal_text'    => __( 'Attach', 'attachments' ),
   
           // which tab should be the default in the modal (string) (browse|upload)
           'router'        => 'browse',
   
           // fields array
           'fields'        => $fields,
   
         );
   
         $attachments->register( 'attachments', $args ); // unique instance name
       }
   
       add_action( 'attachments_register', 'attachments' );
       ```
   
 * and here is what is in the template file:
 *     ```
       <ul class="grid full-page">
                               <?php 
   
                                   $attachments = new Attachments( 'attachments' ); /* pass the instance name */
                                   $count = 1;
   
                               ?>
                           <?php if( $attachments->exist() ) { 
   
                               ?>
   
                               <li <?php post_class('grid-item-copy'); ?> id="post_<?php the_ID(); ?>">
                                   <?php echo wpautop(get_the_content(), true); ?>
                                   <div class="social">
                                       <div class="fb-like" data-href="<?php the_permalink() ?>" data-send="true" data-layout="button_count" data-width="330" data-show-faces="false"></div>
                                   </div>
                               </li>
   
                               <?php while( $attachments->get() ) : ?>
   
                               <li<?php echo (++$count%3 ? '' : ' class="end-of-row"'); ?>>
                                   <h2>
                                       <a href="<?php echo $attachments->src( 'full' ); ?>" class="colorbox">
                                           <span class="item-image" style="background: url(<?php echo get_template_directory_uri() .'/scripts/timthumb.php?w=330&h=330&src='. $attachments->src(' full '); ?>) 0 0 no-repeat;">
                                               <img src="<?php echo get_template_directory_uri() .'/scripts/timthumb.php?w=330&h=330&f=2&src='. $attachments->src( 'full' ); ?>" alt="<?php echo trim(strip_tags( $attachments->field( 'caption' ) )); ?>" />
                                           </span><?php
                                               $profile_title = trim(strip_tags( $attachments->field( 'caption' )));
   
                                               if($profile_title != "") {
                                                   echo '<span class="item-content">'. $profile_title .'</span>';
                                               }
                                       ?></a>
                                   </h2>
                               </li>
                               <?php endwhile; ?>
                           <?php } else { ?>
   
                               <li <?php post_class('grid-item-copy'); ?> id="post_<?php the_ID(); ?>">
                                   <?php the_content(); ?>
                               </li>
   
                           <?php } ?>
                           </ul>
       ```
   
 * if I remove the shuffle() from the return statement then the attachments are 
   shown, if I leave it nothing is shown…
 * Many thanks,
    Tom

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

The topic ‘Random sort order’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/attachments_fafafa.svg)
 * [Attachments](https://wordpress.org/plugins/attachments/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/attachments/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/attachments/)
 * [Active Topics](https://wordpress.org/support/plugin/attachments/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/attachments/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/attachments/reviews/)

 * 9 replies
 * 3 participants
 * Last reply from: [mttd](https://wordpress.org/support/users/mttd/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/random-sort-order/#post-3505994)
 * Status: resolved