[wp-trac] [WordPress Trac] #34691: WP_Posts_List_Table cannot order by meta value correctly

WordPress Trac noreply at wordpress.org
Sun Nov 15 23:03:32 UTC 2015


#34691: WP_Posts_List_Table cannot order by meta value correctly
------------------------------------------+--------------------
 Reporter:  intoxstudio                   |       Owner:
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  4.4
Component:  Administration                |     Version:  trunk
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+--------------------
Changes (by johnbillion):

 * keywords:   => needs-patch needs-unit-tests
 * milestone:  Awaiting Review => 4.4


Old description:

> 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

New description:

 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

--

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34691#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list