Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#23948 closed defect (bug) (invalid)

Random for WP_Query

Reported by: looimaster's profile Looimaster Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.1
Component: Query Keywords:
Focuses: Cc:


I discovered that some servers (like 2 out of 10) don't support random order in WP_Query - MySQL returns posts in the same order all the time. It works on WAMP localhost and on my server but it doesn't work for two other people I'm helping on their servers.

I did the simplest possible WP_Query in Twenty Twelve theme and I discovered that MySQL's RAND() didn't take any effect - it was simply returning posts in the same order all the time (while once again - the same exact WP_Query works on WAMP localhost on Windows 7).

I'm not sure if this qualifies for a bug report.

Change History (10)

#1 @SergeyBiryukov
11 years ago

  • Component changed from General to Query

#2 follow-up: @johnbillion
11 years ago

  • Keywords reporter-feedback added

My only guess to the cause of this is that MYSQL is caching the results in the query cache even though the query contains RAND(). I've never heard of this happening though.

Could you provide the following please:

  • The exact WP_Query query you're using, and the resulting SQL.
  • MySQL version and OS for the affected servers.
  • Details of the MySQL query cache on the affected servers, if any.

#3 in reply to: ↑ 2 @kovshenin
11 years ago

Replying to johnbillion:

My only guess to the cause of this is that MYSQL is caching the results in the query cache even though the query contains RAND(). I've never heard of this happening though.

That'd be strange indeed. Keywords such as NOW(), CURRENT_TIMESTAMP() and RAND() all by-pass query cache. If I had to guess, I'd say that maybe the 2/10 servers have page cache enabled, or perhaps the results of WP_Query are being stored into persistent object cache. In any case, need more details.

Last edited 11 years ago by kovshenin (previous) (diff)

#4 @Looimaster
11 years ago


Image with my explanation to a person having difficulties:

Test case:

<?php $query = new WP_Query( array ( 'post_type' => 'portfolio', 'orderby' => 'rand', 'order' => 'DESC' ) ); var_dump($query); ?>

Note that this is a custom post type.

#5 @kovshenin
11 years ago

Can you also try and set ignore_sticky_posts to true?

#6 @Looimaster
11 years ago

I don't have access to this server anymore, I'm sorry! I tested this for that person 30 days ago and today I decided to report this as a bug just in case. That person didn't have sticky posts. He had just a few custom post type posts.

It works on my WAMP and on the server that I'm using though (not

#7 @johnbillion
11 years ago

OK, WP Engine uses very aggressive caching all over the place. I would not be surprised to find out this is a problem due to WP Engine's configuration. They use Varnish for page caching and Memcached for object caching. I have access to a WP Engine account that I can test this on.

#8 @johnbillion
11 years ago

WP Engine has an Advanced Configuration section in their control panel that shows that, by default, RAND() is disabled for performance reasons. The setting can be found on the 'WP Engine' menu item in the dashboard of any WP Engine hosted site. See screenshot below.

Looimaster, could you mention this to your client please and see if enabling RAND() from here fixes the issue? If so, I think it's safe to close this ticket as invalid due to the hosting environment.

#9 @Looimaster
11 years ago

Thank you for your investigation :)

This case has been closed 30 days ago and he believes that it's up to server configuration because my explanation about WAMP and WPMU-based trial website convinced him but I notified him anyway. I'm not sure if he'll get back to me.

With 99.999% probability that's what is wrong so I believe it's safe to close this ticket now as invalid.

#10 @SergeyBiryukov
11 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.