Sorting and filtering on custom columns

This is a 2-part post discussing how to set up custom columns for a custom post type in WordPress. Part 1 focuses on adding custom columns, displaying them in the edit posts screen, and adding the admin links to one of those columns. Part 2 discusses how to enable sorting and filtering on those custom columns.

WordPress provides a hook that allows you to define which columns are sortable. There’s a generic one and another one for custom post types:

  • manage_edit-post_sortable_columns
  • manage_edit-{$post_type}_sortable_columns

Although there appears to be no Codex page to document either of these hooks, both scribu and Justin Tadlock have published excellent articles explaining how to use them to make your columns sortable.

The other hook we’ll need is pre_get_posts which allows us to define custom callbacks to tell WordPress how to sort and filter our columns.

First we add the hooks:

The first function simply defines which columns should be sortable. Just add your custom sortable columns to the array and/or remove any columns that you don’t want to be sortable:

Now WordPress knows which columns should be sortable. However, it doesn’t automatically understand how to sort them, so you have to provide a function to handle that process.

In this function you have access to the query object, which you can then alter to perform whatever kind of sorting you want. In this example, the data for the custom columns are stored as post_meta values. First we get the column to be sorted on from the query string, and then we tell the query to sort on that meta key. You can easily adapt this to your particular sorting needs by using the appropriate WP_Query parameters.

The last function is similar. Again, the strategy is to alter the query object so that the list gets filtered on the value(s) we want. First we determine which columns are present in the query string, and then we add a meta query for each one (although I think it only lets you sort by one column at a time).

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">