WordPress.org

Make WordPress Core

Opened 10 years ago

Closed 9 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 10 years ago.
4617b.diff (1.2 KB) - added by Nazgul 9 years ago.
rand.diff (1.2 KB) - added by Otto42 9 years ago.
Patch using switch/case for higher speed

Download all attachments as: .zip

Change History (14)

#1 @Nazgul
10 years ago

  • Keywords has-patch added

Added a patch based on the given code snippet.

#2 @Otto42
10 years ago

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

My mistake, typo in the original post.

@Nazgul
10 years ago

#3 @Nazgul
10 years ago

Path updated to remove the bogus ` mark.

#4 follow-up: @Otto42
10 years ago

  • Keywords 2nd-opinion added

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

#5 in reply to: ↑ 4 @weefz
9 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.

#6 @xurizaemon
9 years ago

  • Cc xurizaemon@… added

++

i'd love to have this in core

#7 @ryan
9 years ago

  • Milestone changed from 2.3 to 2.4 (next)

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

@Nazgul
9 years ago

#8 @Nazgul
9 years ago

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

Refreshed patch

@Otto42
9 years ago

Patch using switch/case for higher speed

#9 @Otto42
9 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.

#10 @ryan
9 years ago

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

#11 @ryan
9 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.