Generate repeatable random post ordering
-
Hello,
I am the author of the Image Wall plugin. In short, it displays all images in a wordpress site on an infinitely scrollable page with each image linking back to its parent post or page.
The Problem
The images are batched so that the plugin doesn’t try to load EVERY image in one go. New images are displayed as you scroll down, bringing in more and more batches of images.
The images are also displayed in random order. This means that the random order of the images has to be ‘remembered’ across pagination requests to avoid duplicates.
Current Solution
I’ve solved this issue for now by generating a hash of the image ID (plus a time stamp) and attaching this hash as a meta data to each image, and then retrieving the images ordered by this hash.
The code below is part of what gets run when the plugin is first activated, creating and attaching the hashes to all media files.
$myposts = get_posts( array( 'post_type' => 'attachment', 'nopaging' => true, 'post_status' => 'all', 'post_parent' => null ) ); $tmn_time = time(); foreach( $myposts as $mypost ) { update_post_meta($mypost->ID, 'tmn-iw-hash', md5($mypost->ID + $tmn_time)); }
And then I bring back the images using the following query:
$iw_query = new WP_Query( array( 'post_type' => 'attachment', 'post_mime_type' =>'image', 'posts_per_page' => $batch_size, 'nopaging' => false, 'post_status' => 'all', 'meta_key' => 'tmn-iw-hash', 'orderby' => 'meta_value', 'paged' => $tmn_page ) );
Problem with the current solution.
The problem I have, and hope you might help me with, is that the task of bringing back every image in wordpress, loop over them, generating hashes and attaching them one by one is not very scalable. It takes time, and during this time, no images are displayed leaving the plugin users to think that it isn’t working. I’ve also had reports of the process not finishing.
I’m open to any creative idea that can help make this more efficient and scalable.
I have two approaches, but not enough coding skill to figure them out myself.
1. Execute the hashing in one fast and efficient SQL statement.
If there was some SQL statement that could generate a new hash for every image and update the meta-data table without bringing all the images back and looping over them in PHP, that might be more scalable.
Trouble is, I don’t know SQL very well. I don’t even know if this is feasable.
2. Ditch the hash, bring back the images in a random order and save it for later retrieval.
If I could use the query argument `orderby=rand’ and somehow save the random order it generates so I can still use paging, that would be much better. Only, I don’t know how this might be done.
If you can figure this out, please let me know!
Thank you for sticking with this overlong plea for help!
Regards,
Gustav
- The topic ‘Generate repeatable random post ordering’ is closed to new replies.