WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#4617 closed enhancement (fixed)

Allow random post ordering

Reported by: Otto42 Owned by: ryan
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.2.1
Component: Template Keywords: has-patch
Focuses: Cc:

Description

This change is to allow orderby=rand for all the post query functions.

Changes needed:
In wp-includes, query.php, add "rand" to the list of $allowed_keys.

Modify this code

if ( 'menu_order' != $orderby )
$orderby = 'post_' . $orderby;

into this:

if ( 'menu_order' != $orderby && 'rand' != $orderby)
$orderby = 'post_' . $orderby;
if ('rand' == $orderby)
$orderby = 'RAND()';`

This will allow the mysql "ORDER BY RAND()" to be used in querying posts. There are lots of potential uses for pulling random posts, and current plugins to do it always have to directly query the database for them, meaning that they break whenever the database structure changes. Providing this sort of functionality directly makes more sense.

Since this is a simple change that doesn't break anything and only adds minor functionality, I suggest getting it in quickly. Preferably trunk. I would submit a patch, but I can't right now and anyway it's a simple change.

Attachments (3)

4617.diff (1.1 KB) - added by Nazgul 7 years ago.
4617b.diff (1.2 KB) - added by Nazgul 6 years ago.
rand.diff (1.2 KB) - added by Otto42 6 years ago.
Patch using switch/case for higher speed

Download all attachments as: .zip

Change History (14)

comment:1 Nazgul7 years ago

  • Keywords has-patch added

Added a patch based on the given code snippet.

comment:2 Otto427 years ago

Patch has an extra ` mark after this line:
$orderby = 'RAND()';`

My mistake, typo in the original post.

Nazgul7 years ago

comment:3 Nazgul7 years ago

Path updated to remove the bogus ` mark.

comment:4 follow-up: Otto427 years ago

  • Keywords 2nd-opinion added

Anybody like this? Dislike it? It would be nice to get into the core code..

comment:5 in reply to: ↑ 4 weefz7 years ago

Replying to Otto42:

Anybody like this? Dislike it? It would be nice to get into the core code..

I'm new to this Trac system so not sure how it all works but I'd be very happy if this functionality were made available.

comment:6 xurizaemon7 years ago

  • Cc xurizaemon@… added

++

i'd love to have this in core

comment:7 ryan7 years ago

  • Milestone changed from 2.3 to 2.4 (next)

I'm cool with rand, but let's wait until 2.4.

Nazgul6 years ago

comment:8 Nazgul6 years ago

  • Keywords 2nd-opinion removed
  • Owner changed from anonymous to Nazgul
  • Status changed from new to assigned

Refreshed patch

Otto426 years ago

Patch using switch/case for higher speed

comment:9 Otto426 years ago

Any traction on this? Can we get this into the core? I see people on the support forums having to do rather convoluted and easily broken SQL to accomplish this goal currently, and this simple patch would eliminate that need entirely.

Attached patch for trunk using a switch/case instead of multiple comparisons, for speed reasons.

comment:10 ryan6 years ago

  • Owner changed from Nazgul to ryan
  • Status changed from assigned to new

comment:11 ryan6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [6760]) Allow rand post ordering. Props Otto42 and Nazgul. fixes #4617

Note: See TracTickets for help on using tickets.