Opened 9 years ago
Closed 9 years ago
#34691 closed defect (bug) (duplicate)
WP_Posts_List_Table cannot order by meta value correctly
Reported by: | intoxstudio | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.4 |
Component: | Administration | Keywords: | needs-patch needs-unit-tests |
Focuses: | Cc: |
Description (last modified by )
Sorting by meta_value in tables that extend the WP_Posts_List_Table class does no longer work as expected. Previously it would be done by adding the column in the manage_edit-{$post_type}_sortable_columns filter and then injecting something like following into the posts query:
array( 'meta_key' => 'some_key', 'orderby' => 'meta_value' )
Then, when you click on a column header, ascending and descending sorting would be handled automatically.
Not anymore. If you click on a column header with sorting by meta_value, it only works ascending (or descending if this is default).
This is caused by this function in WP_Posts_List_Table:
protected function get_orderby() { return strtolower( get_query_var( 'orderby' ) ); }
It will return "meta_value" when sorting by a meta value, and thus the column will never be recognized when rendering the sortable header (unless its name is in fact meta_value).
Before WP4.4, the check would be on $_GET["orderby"]
that would return the actual sorting key, in this example "some_key".
I like the idea of the new abstraction of get_orderby(), but why was it changed from $_GET
? Would anything break if we changed it back?
E.g.:
protected function get_orderby() { return strtolower( isset($_GET["orderby"]) ? $_GET["orderby"] : "" ); }
Probably related: #34479
get_orderby()
was introduced in [34728]. See #25493.This appears to be the same issue as what's described in https://core.trac.wordpress.org/ticket/25493#comment:9, so I'm going to mark this ticket as a duplicate. @intoxstudio - please follow along on that ticket, and thanks for the report.