Title: Displaying Random Images
Last modified: August 31, 2016

---

# Displaying Random Images

 *  Resolved [Philbeaux](https://wordpress.org/support/users/philbeaux/)
 * (@philbeaux)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/displaying-random-images/)
 * Would it be possible to create a widget with MLA to display random images from
   recently uploaded images and then in turn link the image to the post that contains
   the respective image?
 * [https://wordpress.org/plugins/media-library-assistant/](https://wordpress.org/plugins/media-library-assistant/)

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

 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/displaying-random-images/#post-7207732)
 * Thanks for an interesting question, which breaks down into three parts. I will
   answer each part in turn.
 * First, you wrote “**_Would it be possible to create a widget with MLA …_**“. 
   You can place the `[mla_gallery]` shortcode in any widget that accepts and processes
   shortcodes. In particular, you can use the “MLA Text” shortcode provided by MLA
   itself. Navigate to the Appearance/Widgets admin submenu and look for the “MLA
   Text” widget. Add it to the sidebar or widget area of your choice, then open 
   it up and add the `[mla_gallery]` shortcode and any other content you need.
 * Second, you want to “**_link the image to the post that contains the respective
   image_**“. This is one of the examples given in the “Gallery Display Content”
   section of the Settings/Media Library Assistant Documentation tab. You can use
   the `mla_link_href` parameter to change the link behind each gallery item. For
   your application, the parameter would be:
 *     ```
       mla_link_href='{+site_url+}/?page_id={+parent+}'
       ```
   
 * Finally, you want to “**_display random images from recently uploaded images_**“,
   and display only those images already attached to a post. This is the most challenging
   part of your question because “**_display random images_**” and “**_recently 
   uploaded images_**” both require limiting the number of items and sorting them
   in a different way. The SQL query required for this application is beyond the
   capability of the [WordPress WP_Query class](https://codex.wordpress.org/Class_Reference/WP_Query)
   that MLA uses to select items for `[mla_gallery]`.
 * You can use the hooks MLA provides to substitute your own custom SQL query and
   accomplish your goal. I have developed the code required and added it to one 
   of the example plugins included with MLA. The example code is activated by a 
   custom shortcode parameter that specifies how many items are considered “recent”
   and optionally, how many of those to display. For example, to display just one
   of the six most recent items you would code `recent_random_uploads="6"' or to
   display two of them you would code`recent_random_uploads=”6,2″`.
 * The shortcode you would add to your widget would be:
 *     ```
       [mla_gallery recent_random_uploads="6,2" mla_link_href='{+site_url+}/?page_id={+parent+}']
       ```
   
 * You can add the PHP code required to perform the query to your theme’s `functions.
   php` file or use a custom plugin such as the `/plugins/media-library-assistant/
   examples/mla-hooks-example.php.txt` example plugin. Here is an excerpt of the
   example plugin with just the code required for your application:
 *     ```
       <?php
       class MLAGalleryHooksExample {
           public static function initialize() {
               if ( is_admin() )
                   return;
   
               add_filter( 'mla_gallery_attributes', 'MLAGalleryHooksExample::mla_gallery_attributes_filter', 10, 1 );
               add_filter( 'mla_gallery_query_arguments', 'MLAGalleryHooksExample::mla_gallery_query_arguments_filter', 10, 1 );
           }
   
           private static $shortcode_attributes = array();
   
           public static function mla_gallery_attributes_filter( $shortcode_attributes ) {
               self::$shortcode_attributes = $shortcode_attributes;
               return $shortcode_attributes;
           } // mla_gallery_attributes_filter
   
           public static function mla_gallery_query_arguments_filter( $all_query_parameters ) {
               if ( isset( self::$shortcode_attributes['recent_random_uploads'] ) ) {
                   global $wpdb;
   
                   // Extract the number of "recent posts" to consider and the (optional) number to display
                   $limits = explode( ',', self::$shortcode_attributes['recent_random_uploads'] );
                   $recent_limit = absint( $limits[0] );
                   if ( 0 == $recent_limit ) {
                       return $all_query_parameters;
                   }
   
                   $display_limit = isset( $limits[1] ) ? absint( $limits[1] ) : 1;
                   if ( 0 == $display_limit ) {
                       $display_limit = 1;
                   }
   
                   // Build an array of SQL clauses
                   $query = array();
                   $query_parameters = array();
   
                   $query[] = "SELECT p.ID FROM (";
                   $query[] = "SELECT ID FROM {$wpdb->posts} WHERE (";
                   $query[] = "( post_type = 'attachment' )";
                   $query[] = "AND ( post_status = 'inherit' )";
                   $query[] = "AND ( post_parent > 0 )";
                   $query[] = "AND ( post_mime_type LIKE 'image/%%' )";
                   $query[] = ") ORDER BY post_date DESC";
   
                   $query[] = "LIMIT %d";
                   $query_parameters[] = $recent_limit;
   
                   $query[] = ") AS p ORDER BY RAND()";
   
                   $query[] = "LIMIT %d";
                   $query_parameters[] = $display_limit;
   
                   $query =  join(' ', $query);
                   $ids = $wpdb->get_results( $wpdb->prepare( $query, $query_parameters ) );
                   if ( is_array( $ids ) ) {
                       $includes = array();
                       foreach ( $ids as $id ) {
                           $includes[] = $id->ID;
                       }
                       $all_query_parameters['include'] = implode( ',', $includes );
                   } else {
                       $all_query_parameters['include'] = '1'; // return no images
                   }
   
                   // Remove redundant parameters from the final query
                   $all_query_parameters['post_mime_type'] = 'all';
                   $all_query_parameters['orderby'] = 'none';
                   $all_query_parameters['post_status'] = 'all'; 
   
                   return $all_query_parameters;
               } // parameter "recent_random_uploads" is present
   
               return $all_query_parameters;
           } // mla_gallery_query_arguments_filter
       } // Class MLAGalleryHooksExample
       add_action('init', 'MLAGalleryHooksExample::initialize');
       ?>
       ```
   
 * If you add the above code to your theme’s `functions.php` file or another suitable
   file you will get the `recent_random_uploads` feature. You can instead install
   and activate the example plugin if you want to separate this feature from other
   code on your site. The example plugin has additional functions and lots of documenting
   comments I have removed from the above code.
 * I have uploaded a new MLA Development Version dated 20160325 that contains the
   above code in the `mla-hooks-example.php.txt` example plugin. To get the Development
   Version, follow the instructions in this earlier topic:
 * [Shortcode not working in (special) widget](https://wordpress.org/support/topic/shortcode-not-working-in-special-widget?replies=3#post-7753687)
 * You can follow the instructions in the “MLA Gallery Filters and Actions (Hooks)”
   section of the /Settings/Media Library Assistant Documentation tab to access 
   the example. I can also send you a copy if that would be easier for you to use.
   You can give me your contact information using the Contact Us page at the FTJ
   web site:
 * [Fair Trade Judaica/Contact Us](http://fairtradejudaica.org/our-story/contact-us/)
 * Do not post your e-mail address in the forum; personal details in a public forum
   violates WordPress guidelines. If you have trouble accessing the FTJ site, post
   a note here with your country of origin and I can temporarily unblock it.
 * I am marking this topic resolved, but please update it if you have any problems
   or further questions regarding the example I developed. Thanks for your interest
   in the plugin.
 *  Thread Starter [Philbeaux](https://wordpress.org/support/users/philbeaux/)
 * (@philbeaux)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/displaying-random-images/#post-7207801)
 * David,
    Thank You so much. I turned the php code you provided into a plugin and
   it seems like it’s working fine. Ok, now to fine tune the display in the widget
   I assume I’ll edit the MLA short code?
 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/displaying-random-images/#post-7207810)
 * Thanks for your update with the good news.
 * Yes, you can make many format adjustments with the “Gallery Display Style” and“
   Gallery Display Content” parameters for the `[mla_gallery]` shortcode. You can
   gain complete control over the styles and markup by using “Style and Markup Templates”.
   All of these options are covered in the Documentation tab. If you have problems
   or questions, update this topic or start a new topic.
 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/displaying-random-images/#post-7208081)
 * I have uploaded an MLA Development Version dated 20160802 that contains a completely
   new approach to browsing and installing the MLA Example Plugins. If you navigate
   to the Settings/Media library Assistant Documentation tab and click the “Example
   Plugins” button you will see a new submenu that lists all the example plugins
   and give you a “one-click” action for installing them. I hope this will make 
   future installations of the example plugins more convenient for all MLA users.
   Thanks for helping to inspire this MLA enhancement.
 *  Thread Starter [Philbeaux](https://wordpress.org/support/users/philbeaux/)
 * (@philbeaux)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/displaying-random-images/#post-7208082)
 * Thanks David.
    As soon as I have some time I will look into this.

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

The topic ‘Displaying Random Images’ is closed to new replies.

 * ![](https://ps.w.org/media-library-assistant/assets/icon-256x256.png?rev=973502)
 * [Media Library Assistant](https://wordpress.org/plugins/media-library-assistant/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/media-library-assistant/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/media-library-assistant/)
 * [Active Topics](https://wordpress.org/support/plugin/media-library-assistant/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/media-library-assistant/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/media-library-assistant/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Philbeaux](https://wordpress.org/support/users/philbeaux/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/displaying-random-images/#post-7208082)
 * Status: resolved