DocumentationOnline documentation for Ajax Load More
Random Ordering
Out of the box Ajax Load More can not order posts by random, however it can be achieved by running a custom WP_Query that builds an array of post IDs – the post IDs are then passed into the post__in shortcode parameter for display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $post_ids = []; // Query ALL posts ordered by random $args = [ 'post_type' => [ 'post' ], 'orderby' => 'rand', 'posts_per_page' => -1, 'ignore_sticky_posts' => true, ]; $the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : $the_query->the_post(); // Build array of post IDs. $post_ids[] = $post->ID; endwhile; wp_reset_query(); // Pass $post_ids array to Ajax Load More post__in parameter. echo do_shortcode('[ajax_load_more post__in="'. implode(',', $post_ids) .'" orderby="post__in"]'); ?> |
Note: Ordering by random is very server intensive and some hosts (e.g. WPEngine) disallow this orderby query parameter completely.