Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#41350 closed feature request (wontfix)

Support for random order in WP_Term_Query::query()

Reported by: camthor's profile camthor Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: Priority: normal
Severity: normal Version: 4.8
Component: Taxonomy Keywords: good-first-bug has-patch has-unit-tests
Focuses: Cc:

Description

As already valid and useful for posts, it should be possible to choose a random order also for terms.

This could be achieved by adding "rand" as additional allowed value for "orderby". The function "parse_orderby" would need to be modified, translating "orderby => "rand" into "ORDER BY RAND()"

Attachments (5)

41350-order-by-rand.diff (3.1 KB) - added by bretterer 7 years ago.
41350.2.diff (2.4 KB) - added by bretterer 7 years ago.
Removed random and rand() from being options.
41350.3.diff (2.3 KB) - added by bretterer 7 years ago.
Fixed Spaces and converted them to Tabs
41350.4.diff (2.3 KB) - added by bretterer 7 years ago.
Fixed comment spacing issues
41350.5.diff (2.3 KB) - added by bretterer 7 years ago.
Fixes test docblock

Download all attachments as: .zip

Change History (19)

#1 @boonebgorges
7 years ago

  • Keywords good-first-bug needs-patch added

Hi @camthor - Thanks for the ticket, and welcome to WordPress Trac!

orderby=rand is a pattern that we support in WP_Query, so it seems fine to support it here as well. Would you be interested in working on a patch?

#2 @bretterer
7 years ago

  • Keywords has-patch added; needs-patch removed

Hello,

I took a stab at this ticket. I believe this covers the case stated in the ticket for allowing "orderby" => "rand" to be added to a new term_query.

I went one step further and now allow rand, random, or rand() to be passed into the orderby property. If any of these are passed, the query will change the ORDER BY from name to rand()

Tests have been written as well to test for each of these properties.

Please let me know if this is correct.

-Brian

#3 @bretterer
7 years ago

  • Keywords has-unit-tests added

#4 follow-up: @theMikeD
7 years ago

Probably best if we just stick with "orderby" => "rand" The extra complexity isn't required.

#5 in reply to: ↑ 4 @bretterer
7 years ago

Replying to theMikeD:

Probably best if we just stick with "orderby" => "rand" The extra complexity isn't required.

I was on the fence if I add the extras... I will remove them and re-submit a patch.

@bretterer
7 years ago

Removed random and rand() from being options.

This ticket was mentioned in Slack in #core by bretterer. View the logs.


7 years ago

@bretterer
7 years ago

Fixed Spaces and converted them to Tabs

@bretterer
7 years ago

Fixed comment spacing issues

@bretterer
7 years ago

Fixes test docblock

This ticket was mentioned in Slack in #core by bretterer. View the logs.


7 years ago

#8 @SergeyBiryukov
7 years ago

  • Milestone changed from Awaiting Review to 4.9
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#9 @JPry
7 years ago

While this is indeed already present for querying posts, I would like to vote against including this change. It may be a nice idea for API consistency, but this becomes one more place where a developer who is ignorant of the performance ramifications can cause problems.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 years ago

#11 @johnbillion
7 years ago

  • Milestone 4.9 deleted
  • Resolution set to wontfix
  • Status changed from reviewing to closed

I agree with @JPry that this sort of usage should not be encouraged. Ordering by rand() has a considerable impact on database performance. Consistency of APIs is great, but consistency of functionality which can kill a server's performance isn't.

Thanks for the suggestion, @camthor, and thanks very much for the patches @bretterer, but I'm going to close this one as a wontfix.

#12 @bretterer
7 years ago

@johnbillion I disagree with the closing of this ticket. Not because I wrote the patch, but because of the consistency.

My suggestion here is that this does get included, but in the core WP Query class (or where appropriate) we add some functionality that most hosting providers and look for ORDER BY rand() and replace it if the explicit option to enable rand() is enabled.

This allows us to have a place to educate users of the issues with rand() if they are unaware and have to specifically state they are ok with the server killing usage of it.

#13 @camthor
7 years ago

So this is already closed? I don't quite understand the argument to prevent wrong coding practices on the API level. There is plenty of possibilities where server performance can be negativly affected, and I don't see why rand() for terms crosses that line, while rand() for posts doesn't. If the code is badly designed, it is better to improve it, rather than "locking away" resource intensive functions from the people. Somehow it feels strange to me to promote Wordpress as "open" if we at the same time "incapacitate" the programmers because they could possibly do something wrong. After all, if a plugin is a resource hog, all you need to do is deactivate it.
OK, that's just my 2 cents, since I started the ticket. I will, of course, accept your decision, you have far more experience than I. :-)

#14 @SergeyBiryukov
7 years ago

Just wanted to note that even if WP_Term_Query::parse_orderby() does not explicitly support orderby => 'rand', support for it can be added using get_terms_orderby filter.

Note: See TracTickets for help on using tickets.