DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Change Sortable Columns In WordPress

Change Sortable Columns In WordPress

Paul Underwood user avatar by
Paul Underwood
·
Feb. 06, 15 · Web Dev Zone · Interview
Like (0)
Save
Tweet
7.47K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous tutorial we saw how you can recreate the All Posts table in the WordPress admin area by using the WP_List class in WordPress.

It talks about creating your own table and populating it with any data that you need. It talks about how you can customise the table to behaviour in anyway that is needed by adding different columns to the table, assigning sortable columns to the table, pagination and searching the data, just like you can in the All posts table.

But in this previous tutorial only explains how to create sortable columns on a new table, in this tutorial we are going to look at changing an existing table to apply new sortable columns.

To edit the columns and change them to be sortable we need to use a filter and return is a set of column IDs of what columns we want to be sortable. The filter we need to use is manage_edit-{$this->screen->id}_sortable_columns.

add_filter( 'manage_edit-{$this->screen->id}_sortable_columns', 'manage_sortable_columns');

The below code snippet will change the author column on the posts screen to be sortable.

class Pu_Manage_Sortable_Columns
{
    public function __construct()
    {
        add_filter( 'manage_edit-post_sortable_columns', array($this, 'manage_sortable_columns'));
    }

    public function manage_sortable_columns( $columns )
    {
        $columns['author'] = 'Author';

        return $columns;
    }
}

Now you will see that the author column is sortable which is noticeable by the sort arrow appearing next to the Author title on the hover event. Clicking on the author title will now sort the table data by the Author name.

Database WordPress

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Guide to Understanding Vue Lifecycle Hooks
  • A Guide to Events in Vue
  • Image Classification Using SingleStore DB, Keras, and Tensorflow
  • Top 20 Git Commands With Examples

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo