Skip Navigation

[Resolved] View not displaying in random order

This support ticket is created 7 years, 6 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.

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by joshM-4 7 years, 6 months ago.

Assisted by: Konstantinos Galanakis.

Author
Posts
#446340
Screen Shot 2016-10-13 at 1.11.00 PM.png

I am trying to: get a view to display posts (businesses) in a random order

I visited this URL: herocardonline.com

I expected to see: the posts in the "Featured Businesses" area at the bottom loading different posts with each page refresh

Instead, I got: the same 4 posts with each page refresh

----

A couple other notes.
1. This function works on my local dev environment, but not the live host (WP Engine)
2. WP Engine utilizes caching. If we can identify the query that is made to load these posts at random, I can have them exclude that query from their caching system. This is really what I'm looking for with this request. I don't know how to find that query.

#446478
enable-wpengine-order-random-wp.png

Hello Josh.

Thank you for contacting the Toolset Support.

WPEngine has by default disabled random order for WordPress queries but it can be enabled right via your dashboard. For more information on how to enable this, look at the attached screenshot.

If this doesn't resolve your issue or you have any further issues or questions, please let me know.

Regards

#446801

I have to say I'm very impressed that you found that!

I gave it a go and it would display at random for only 1 refresh. So I contacted WP Engine support and the very helpful guy spent about an hour with me trying to figure out how to isolate that query and exclude it from caching (he said it was still getting caught in the Varnish cache).

In the end, he replied with the following as a possible solution: "Alright so it looks like no matter how we exclude the cookie for this, since its php it pulls the entire home page out of cache. On our servers they wont let us un cache the homepage since this takes the most hits. For this I would have 2 recommendations. You can see if creating this with JavaScript is an option and it will be much easier to exclude, or if you are able to maybe contact the developer of that plugin and see the exact content to exclude we could do that too."

These solutions are a little bit past me. Does what he's saying here make sense to you? Do you know what he's referring to here?

To be honest, I thought this was was controlled with JavaScript. Do you know what content I need to give them for exclusion in the cache?

I sure do appreciate the help!

#447380

Hello Josh.

As you understand, this is clearly a WP Engine issue as they choose to cache the homepage, where the "featured businesses" view appears. Caching the homepage, of course, is the most appropriate thing to do as it's the page that receives most hits, but this actually makes the random ordering of those posts impossible.

The results for this "featured businesses" are fetched from the database using PHP and MySQL, so in order to get that random ordering on posts, you will most probably have to stop caching the relevant MySQL query, if it's possible. The query you are looking for should be similar to the one below

SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND wp_posts.ID NOT IN (178) AND wp_posts.post_type = 'business' AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))  ORDER BY RAND() 

I haven't tested that before, so I cannot tell you that it will work or not, but if you test it, please let me know about the results.

If you have any further issues, don't hesitate to ask.

Regards

#448031

Thanks for the continued help, Konstantinos.

Looks like the short story on this is that it can take up to a couple days for the Allow ORDER BY RAND() to start working properly in WP Engine.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.