Title: The problem with randomizing posts
Last modified: May 30, 2023

---

# The problem with randomizing posts

 *  Resolved [leowebmarketing](https://wordpress.org/support/users/leowebmarketing/)
 * (@leowebmarketing)
 * [3 years ago](https://wordpress.org/support/topic/the-problem-with-randomizing-posts/)
 *     ```wp-block-code
       Hello. I am working on a project for my client 
       This listing website uses WordPress, Elementor, Essential Addons for Elementor - Pro
   
       With Essential Addons for Elementor - Pro, similar pages are displayed on pages using post grid (pages have categories), and similar pages are displayed on listing pages using Essential Addons for Elementor - Pro and "Posts Carousel" and "Post Block". The problem is that Posts Carousel, Post Block, Post grid display the same pages, but randomize only their order. I need the Posts Carousel, Post Block, Post grid widgets on Elementor pages to display randomized posts with a similar category as the current post (page or listing). When the page refreshes, this list should change. I came up with a way to double randomize. If I need to display 4 posts in a Post grid on an elementor page, first, 10 pages with the same category as the current page are randomly selected. After that, 4 pages are randomly selected from them. Here is the code for this function random_related_pages() {
       global $post;
       // Get the current page categories
       $current_page_categories = wp_get_post_terms($post->ID, 'category', array("fields" => "ids"));
   
       // Create a query
       $args = array(
       'post_type' => 'page',
       'tax_query' => array(
       array(
       'taxonomy' => 'category',
       'terms' => $current_page_categories,
       ),
       ),
       'posts_per_page' => 10, // first, get 10 random pages
       'orderby' => 'rand',
       );
       $random_pages_query = new WP_Query($args);
   
       $output = '';
       if ($random_pages_query->have_posts()) {
       // Get all 10 pages in an array
       $all_pages = array();
       while ($random_pages_query->have_posts()) {
       $random_pages_query->the_post();
       $all_pages[] = '
   
       ' . get_the_title() . '
   
       ';
       }
   
       // Randomly pick 4 from the array
       $picked_pages = array_rand($all_pages, 4);
   
       // Output the picked pages
       foreach ($picked_pages as $index) {
       $output .= $all_pages[$index];
       }
       } else {
       // No pages found
       }
       wp_reset_postdata();
   
       return $output;
       }
       add_shortcode('random_related_pages', 'random_related_pages');
       but I need it to change the list of posts for the Posts Carousel, Post Block, Post grid from the Essential Addons for Elementor plugin (these widgets already have a certain design). What can I do? How can I set up a real randomization for these blocks?
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fthe-problem-with-randomizing-posts%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Support [Amin](https://wordpress.org/support/users/amin7/)
 * (@amin7)
 * [3 years ago](https://wordpress.org/support/topic/the-problem-with-randomizing-posts/#post-16781899)
 * Hi [@leowebmarketing](https://wordpress.org/support/users/leowebmarketing/)
 * I hope you are fine today.
 * Regarding your query, we already solved this over our support.
 * A humble request to you, If you don’t mind me asking, could you do me a favor,
   and spend 30-sec to review us following this link, this will help us to serve
   better.
 * Thank you in advance!
 * Link to the review: [https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/reviews/#new-post](https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/reviews/#new-post)

Viewing 1 replies (of 1 total)

The topic ‘The problem with randomizing posts’ is closed to new replies.

 * ![](https://ps.w.org/essential-addons-for-elementor-lite/assets/icon-256x256.
   gif?rev=3182943)
 * [Essential Addons for Elementor - Popular Elementor Templates & Widgets](https://wordpress.org/plugins/essential-addons-for-elementor-lite/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/essential-addons-for-elementor-lite/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/)
 * [Active Topics](https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/essential-addons-for-elementor-lite/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Amin](https://wordpress.org/support/users/amin7/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/the-problem-with-randomizing-posts/#post-16781899)
 * Status: resolved