WordPress.org

Make WordPress Core

#23948 closed defect (bug) (invalid)

Random for WP_Query

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

Description

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)

comment:1 SergeyBiryukov13 months ago

  • Component changed from General to Query

comment:2 follow-up: johnbillion13 months 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.

comment:3 in reply to: ↑ 2 kovshenin13 months 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() are 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.

Version 0, edited 13 months ago by kovshenin (next)

comment:4 Looimaster13 months ago

Server: http://wpengine.com/

Image with my explanation to a person having difficulties:
http://gyazo.com/b1a4990b3a2f83cbda033b7874a0821e.png

Test case:

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

Note that this is a custom post type.

comment:5 kovshenin13 months ago

Can you also try and set ignore_sticky_posts to true?

comment:6 Looimaster13 months 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 wpengine.com).

comment:7 johnbillion13 months 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.

comment:8 johnbillion13 months 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.

http://i.imgur.com/1ltxXNy.png

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.

comment:9 Looimaster13 months 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.

comment:10 SergeyBiryukov13 months 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.