WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 2 weeks ago

#45089 new enhancement

Update WP_List_Table::get_sortable_columns() to support 'asc' and 'desc' arguments

Reported by: Tkama Owned by:
Milestone: 5.2 Priority: normal
Severity: normal Version: 4.9.8
Component: Administration Keywords: has-patch needs-docs needs-testing
Focuses: Cc:

Description

When registering new sortable column we can set default first sort order, by setting false/true in the second parameter of array:

<?php
function get_sortable_columns(){
        return array(
                'register_date' => array( 'register_date', false ), // false = asc
                'login_date'    => array( 'login_date', true ),
                'rating'        => array( 'rating', false ),
        );
}

It is much more clear to write 'asc' / 'desc' then false/true.

To achieve such behavior and not lose current one, we need to change /wp-admin/includes/class-wp-list-table.php#L16

<?php
if ( $current_orderby === $orderby ) {
        $order = 'asc' === $current_order ? 'desc' : 'asc';
        $class[] = 'sorted';
        $class[] = $current_order;
} else {
        $order = $desc_first ? 'desc' : 'asc';
        $class[] = 'sortable';
        $class[] = $desc_first ? 'asc' : 'desc';
}

to

<?php
...
} else {
        if( in_array( strtolower($desc_first), array('desc', 'asc'), true ) )
                $order = strtolower( $desc_first );
        else
                $order = $desc_first ? 'desc' : 'asc';

        $class[] = 'sortable';
        $class[] = $order === 'desc' ? 'asc' : 'desc';
}

Attachments (4)

class-wp-list-table.php.patch (986 bytes) - added by Tkama 4 months ago.
45089.patch (923 bytes) - added by Shital Patel 4 months ago.
create patch
45089.diff (922 bytes) - added by Shital Patel 4 months ago.
45089.2.diff (1.4 KB) - added by Tkama 4 months ago.

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
4 months ago

  • Component changed from General to Administration
  • Keywords has-patch needs-docs added
  • Milestone changed from Awaiting Review to 5.1
  • Summary changed from WP_List_Table to Update WP_List_Table::get_sortable_columns() to support 'asc' and 'desc' arguments

Hi @Tkama, thanks for the patch!

The documentation for WP_List_Table::get_sortable_columns() would also need to be updated to reflect the new accepted value.

@Shital Patel
4 months ago

create patch

@Shital Patel
4 months ago

@Tkama
4 months ago

#2 @pento
5 weeks ago

  • Milestone changed from 5.1 to 5.2

Patch needs review.

#3 @Shital Patel
4 weeks ago

  • Keywords needs-testing added
Note: See TracTickets for help on using tickets.