0

How to show posts in a random order when an archive of a certain category is being showed like as example: mysite.com/category/name-of-specific-category? I have searched everywhere but could not find either code or plugins that could solve this need.

2 Answers 2

-1

To display posts in a random order when viewing an archive of a specific category in WordPress, you can achieve this by creating a custom query using the pre_get_posts action hook. This allows you to modify the default query parameters for category archives.

Here's how you can show posts in a random order for a specific category:

Open your theme's functions.php file: You can access this file via your WordPress dashboard by going to "Appearance" -> "Theme Editor" and selecting your theme's functions.php file.

Add the following code at the end of your functions.php file:

php code

function custom_category_archive_query($query) {
    if (is_category() && $query->is_main_query()) {
        // Replace 'your-category-slug' with the slug of the specific category
        $category_slug = 'your-category-slug';

        // Check if the current query is for the specific category
        if ($query->query_vars['category_name'] == $category_slug) {
            $query->set('orderby', 'rand'); // Set orderby parameter to 'rand' for random order
        }
    }
}
add_action('pre_get_posts', 'custom_category_archive_query');

Make sure to replace 'your-category-slug' with the actual slug of the category for which you want to display posts in random order.

Save the changes to your functions.php file. Now, when you visit the archive page for the specified category, the posts will be displayed in random order.

Please note that using random order for category archives can make it challenging for users to find specific content, as the order will change each time they visit the page. Make sure this is the desired behavior for your website and content before implementing this solution.

2
  • Thank you very much. It is working. Rather than editing the function.php file, I preferred to use WP-Code. Regarding your comments regarding the experience of users visiting the site, I am well aware of the inconvenience, but it is a conscious choice limited to only one category where it is important that the order is random. Thank you again for your help. Commented Sep 3, 2023 at 17:15
  • Seems like this is generate by AI like chatGPT. I think you just copy pasted it straight to here because you forgot to delete the "php code" bit. Commented Sep 4, 2023 at 13:25
0

You can try this:

add_action('pre_get_posts', 'randomize_category_posts');
function randomize_category_posts($query) {
    if ($query->is_main_query() && !is_admin() && is_category('name-of-specific-category')) {
        $query->set('orderby', 'rand');
    }
}

Replace name-of-specific-category with the slug of the actual category you're targeting. Once you add this code to your functions.php, it should work like a charm. Every time the page reloads, the order will be randomized again.

2
  • Thanks a lot. Is working perfectly! Commented Sep 3, 2023 at 16:49
  • No problem! @RenatoGelforte if the answer is perfectly working for you, kindly mark it as accepted so that other people that has similar issue knows where to look. Commented Sep 4, 2023 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.