WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#29629 closed defect (bug) (fixed)

Shortcode playlist random order parameter can't work

Reported by: ialocin Owned by: wonderboymusic
Milestone: 4.1 Priority: normal
Severity: major Version: 4.0
Component: Media Keywords: has-patch
Focuses: Cc:

Description

The parameter RAND for the argument order gets replaced with none

1246            if ( 'RAND' == $atts['order'] ) {
1247                    $atts['orderby'] = 'none';
1248            }

see: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php#L1246

Which gets passed to get_posts() and won't give back a random order

RAND should be replaced by rand - like this:

1246            if ( 'RAND' == $atts['order'] ) {
1247                    $atts['orderby'] = 'rand';
1248            }

than it works perfectly fine.

Discovered while answering a question at WPSE
http://wordpress.stackexchange.com/q/161084/22534

Attachments (2)

29629.diff (1.1 KB) - added by Otto42 5 years ago.
29629.2.diff (3.9 KB) - added by wonderboymusic 5 years ago.

Download all attachments as: .zip

Change History (9)

#1 @nacin
5 years ago

  • Component changed from Shortcodes to Media
  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 4.1

#2 follow-up: @Otto42
5 years ago

It appears that some of this was copied from the gallery shortcode and that the documentation was written somewhat incorrectly.

In short, this is wrong:

 *     @type string  $order        Designates ascending or descending order of items in the playlist.
 *                                 Accepts 'ASC', 'DESC', or 'RAND'. Default 'ASC'.
 *     @type string  $orderby      Any column, or columns, to sort the playlist. If $ids are
 *                                 passed, this defaults to the order of the $ids array ('post__in').
 *                                 Otherwise default is 'menu_order ID'.

To get a random order, the "orderby" parameter should be set to "rand" and the value of the "order" parameter is somewhat irrelevant. It can be ASC or DESC, but it's just sorting random numbers at that point.

These lines can be removed entirely. I don't know why they're in the gallery shortcode or in this shortcode, but they're perfectly useless:

if ( 'RAND' == $atts['order'] ) {
     $atts['orderby'] = 'none';
}

And the documentation in the function header should reflect that "orderby" is what can be "rand", not "order". These values are passed through to the same values in WP_Query, and the parse_query() function there correctly documents their usage.

@Otto42
5 years ago

#3 @Otto42
5 years ago

  • Keywords has-patch added; needs-patch removed

#4 in reply to: ↑ 2 @F J Kaiser
5 years ago

  • Severity changed from normal to major

Replying to Otto42:

These lines can be removed entirely. I don't know why they're in the gallery shortcode or in this shortcode, but they're perfectly useless:

if ( 'RAND' == $atts['order'] ) {
     $atts['orderby'] = 'none';
}

They aren't just useless. They serve as a mine trap. Nice how fast you patched it.

#5 follow-up: @wonderboymusic
5 years ago

Turns out, this is even messier - passing 'orderby' => 'rand' creates queries withRAND() DESC in them.

I made more changes in 29629.2.diff and added some unit tests.

#6 in reply to: ↑ 5 @Otto42
5 years ago

Replying to wonderboymusic:

Turns out, this is even messier - passing 'orderby' => 'rand' creates queries withRAND() DESC in them.

Not surprised, but then I'm inclined to say "who cares"... unless you can show that RAND() DESC is any slower or faster than RAND() ASC. I'm pretty sure that mySQL won't have any difference here.

And even in that case, I think that's probably better suited for another ticket, one more specifically related to improving the performance of WP_Query.

#7 @wonderboymusic
5 years ago

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

In 29760:

Ordering by RAND():

The shortcode callbacks for gallery and playlist check for 'RAND' == $atts['order'], which isn't a valid value for order. Remove those checks and update the docs.

In WP_Query, if the value of orderby is rand, order is irrelevant and should be unset.

Adds unit tests.

Fixes #29629.

Note: See TracTickets for help on using tickets.