Ticket #4617 (closed enhancement: fixed)

Opened 10 months ago

Last modified 3 months ago

Allow random post ordering

Reported by: Otto42 Assigned to: ryan
Priority: normal Milestone: 2.5
Component: Template Version: 2.2.1
Severity: normal Keywords: has-patch
Cc: xurizaemon@gmail.com

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

4617.diff (1.1 kB) - added by Nazgul on 07/12/07 05:21:32.
4617b.diff (1.2 kB) - added by Nazgul on 02/01/08 00:15:41.
rand.diff (1.2 kB) - added by Otto42 on 02/08/08 15:43:13.
Patch using switch/case for higher speed

Change History

07/11/07 21:24:24 changed by Nazgul

  • keywords set to has-patch.

Added a patch based on the given code snippet.

07/11/07 23:24:06 changed by Otto42

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

My mistake, typo in the original post.

07/12/07 05:21:32 changed by Nazgul

  • attachment 4617.diff added.

07/12/07 05:22:21 changed by Nazgul

Path updated to remove the bogus ` mark.

(follow-up: ↓ 5 ) 08/22/07 15:27:42 changed by Otto42

  • keywords changed from has-patch to has-patch 2nd-opinion.

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

(in reply to: ↑ 4 ) 09/11/07 11:32:28 changed by weefz

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.

09/12/07 19:53:12 changed by xurizaemon

  • cc set to xurizaemon@gmail.com.

++

i'd love to have this in core

09/12/07 20:41:30 changed by ryan

  • milestone changed from 2.3 to 2.4 (next).

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

02/01/08 00:15:41 changed by Nazgul

  • attachment 4617b.diff added.

02/01/08 00:16:14 changed by Nazgul

  • keywords changed from has-patch 2nd-opinion to has-patch.
  • owner changed from anonymous to Nazgul.
  • status changed from new to assigned.

Refreshed patch

02/08/08 15:43:13 changed by Otto42

  • attachment rand.diff added.

Patch using switch/case for higher speed

02/08/08 15:45:12 changed by Otto42

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.

02/08/08 16:54:15 changed by ryan

  • owner changed from Nazgul to ryan.
  • status changed from assigned to new.

02/08/08 19:50:11 changed by ryan

  • status changed from new to closed.
  • resolution set to fixed.

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