Opened 17 years ago
Closed 17 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)
Change History (14)
#2
@
17 years ago
Patch has an extra ` mark after this line:
$orderby = 'RAND()';`
My mistake, typo in the original post.
#4
follow-up:
↓ 5
@
17 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
@
17 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.
#7
@
17 years ago
- Milestone changed from 2.3 to 2.4 (next)
I'm cool with rand, but let's wait until 2.4.
#8
@
17 years ago
- Keywords 2nd-opinion removed
- Owner changed from anonymous to Nazgul
- Status changed from new to assigned
Refreshed patch
#9
@
17 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.
Added a patch based on the given code snippet.