The problem with randomizing posts

[ad_1]

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 to see the link]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer