Title: Random Post Doesnt Work
Last modified: June 21, 2023

---

# Random Post Doesnt Work

 *  Resolved [bonumaster](https://wordpress.org/support/users/bonumaster/)
 * (@bonumaster)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/random-post-doesnt-work/)
 * Hi. I tried to execute the random post by using shortcode (because it is not 
   technically possible to put PHP code in gutenberg) but it shows nothing.
 * Here is the code in function.php
 *     ```wp-block-code
       function random_post () {
       $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"]');
       }
       add_shortcode('random_post_order', 'random_post');
       ```
   
 * After the page loads, its just blank. I checked the HTML and it shows that the
   post count is recognized (800+ posts | 800 char of comma showed).
 *     ```wp-block-code
       <div aria-live="polite" aria-atomic="true" class="alm-listing alm-ajax alm-has-transition" data-container-type="div" data-loading-style="blue" data-repeater="default" data-post-type="post" data-post-in=",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,," data-order="DESC" data-orderby="post__in" data-offset="0" data-posts-per-page="5" data-scroll-distance="100" data-button-label="Load More"></div>
       ```
   
 * (It was 800+ of pure comma, i remove some in the comment because it is so long)
   
   Any help is appreciated. Thank you!

Viewing 1 replies (of 1 total)

 *  Thread Starter [bonumaster](https://wordpress.org/support/users/bonumaster/)
 * (@bonumaster)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/random-post-doesnt-work/#post-16837695)
 * To those who are reading this, i fixed this by adding template in the shortcode.

Viewing 1 replies (of 1 total)

The topic ‘Random Post Doesnt Work’ is closed to new replies.

 * ![](https://ps.w.org/ajax-load-more/assets/icon-256x256.png?rev=2944639)
 * [Ajax Load More – Infinite Scroll, Load More, & Lazy Load](https://wordpress.org/plugins/ajax-load-more/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ajax-load-more/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ajax-load-more/)
 * [Active Topics](https://wordpress.org/support/plugin/ajax-load-more/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ajax-load-more/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ajax-load-more/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [bonumaster](https://wordpress.org/support/users/bonumaster/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/random-post-doesnt-work/#post-16837695)
 * Status: resolved