Skip Navigation

[Resolved] Ordering by Random not working

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by leilaG 2 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#2313113
Random.png

Hello, we have set ordering to Random but it's not working
hidden link
Also if it is set to random ordering is it meant to random order on pages that have been filtered. For example if I filter by a location, that new list of results, should be in a random order?
Thanks

#2313241

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2022-03-09 at 07.23.23.png

On my own site random ordering works correctly. To see why it is not working on your site, I would want to enable the Views debug mode, so that I can see the actual query being performed when visiting that page. But I need to be logged-in as an administrator to see the resulting pop-up, so let me set a private reply to get credentials from you.

You should not mix random results with pagination (the classic editor has a warning about this, but it is missing from the block editor). Every time you fetch a new page of results, that is a new database query, which if ordered randomly may include posts that have already been shown on previous pages.

#2314021

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There seems to be a problem with your server, and I think you will need to consult with your hosting company to identify the problem.

The View generates a database query via the WordPress WP_Query class, which creates the actual SQL query. Checking, I can see that the SQL query is correctly formed, including the option to ORDER BY RAND(). But the results are returned ordered by post ID.

I added a plugin (WP Data Access) where you can run database queries directly to demonstrate the problem.

When I run the following simple query, it returns the results ordered by post ID, and not in a random order.

SELECT wp_posts.ID
FROM wp_posts
WHERE wp_posts.post_type = 'company'
AND wp_posts.post_status = 'publish'
ORDER BY RAND()

If the database is returning the results in the wrong order, there's not much we can do about it.

I did a quick Google to see if some server settings were required for random ordering but didn't find anything, and it would be best if you asked your host.

You can show them the results of the above query with the plugin I installed.

Go to WP Data Access > Query Builder, paste in the above query, and Execute.

#2314071

My issue is resolved now. Thank you!

WP Engine MU plugin by default stops ordering by Random. We've now enabled it.