WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#29902 closed enhancement (fixed)

Allow no ORDER BY for comments in WP_Comment_Query

Reported by: psycleuk Owned by: boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 4.0
Component: Comments Keywords: has-patch
Focuses: performance Cc:

Description

The order by filters within WP_Query allow completely removing any order by appearing within the resultant posts database query, the same is true for the comments feed query in WP_Query. However it isn't the case for WP_Comment_Query.

Within WP_Comment_Query the $groupby variable is checked and has the prefix 'GROUP BY ' added. What I propose is the same for $orderby.

This would mean consistency between the two Query classes, and for those plugins that use the comments table for custom comment types, it'd allow removing any order by if the plugin deemed it unnecessary. For example with the latter requesting comments with a post_id, comment_type and user_id triggers a MySQL filesort even if the result is a single entry. Having no ORDER BY would skip the filesort yet have the same result.

My attached patch simply duplicates what the existing $groupby does.

Attachments (1)

comments_orderby.diff (631 bytes) - added by psycleuk 6 years ago.

Download all attachments as: .zip

Change History (5)

#1 @nofearinc
6 years ago

  • Keywords has-patch added

+1, Looks good to me :)

#2 @boonebgorges
6 years ago

In 30003:

Add unit tests for WP_Comment_Query 'orderby' param.

For better testability, the SQL string generated in WP_Comment_Query::get_posts()
is now stored as a 'request' property on the object.

See #29902.

#3 @boonebgorges
6 years ago

  • Milestone changed from Awaiting Review to 4.1

psycleuk - Thanks very much for the ticket and the patch.

We can't go with exactly your suggestion - the logic https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/comment.php#L318 currently makes it so that $orderby can never be empty, so your if ( $orderby ) condition will always be hit. We'll do what WP_Query does: skip the ORDER BY with boolean false or the word 'none'.

#4 @boonebgorges
6 years ago

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

In 30004:

Allow ORDER BY in WP_Comment_Query::query() to be disabled.

Disable ORDER BY by passing boolean false, an empty array, or the string
'none' to the 'orderby parameter. This mirrors the behavior of WP_Query.

Props psycleuk.
Fixes #29902.

Note: See TracTickets for help on using tickets.