In a recent article by Claudio Simeone, he demonstrated how to add additional columns to a post, or customize the post type, admin screen (or delete an existing one). In this quick tip, I'll build on that and show you how to make a newly created column sortable.
To tell WordPress which columns you want to register as sortable, you need this filter:
manage_{$screen->id}_sortable_column
For posts and pages,$screen->id
are "edit-post" and "edit-page" respectively. Generally speaking, for a post type named "my-post-type", it is "edit-my-post-type".
The filter is passed an array with the name of the sortable column as the key and the sort by as the value. More precisely, these values indicate that the "orderby
" parameter is set in the query that populates the table. In the same way as removing a column, you can also make a column "unsortable" by removing it from this array. Let's look at an example:
Per Claudio's article, assuming we have added the "Slice" column to the "Cake" post type, we can do the following:
add_filter('manage_edit-cake_columns', 'my_extra_cake_columns'); function my_extra_cake_columns($columns) { $columns['slices'] =__('Slices','myplugindomain'); return $columns; }
We added the following content to this column:
add_action( 'manage_cake_posts_custom_column', 'my_cake_column_content', 10, 2 ); function my_cake_column_content( $column_name, $post_id ) { if ( 'slices' != $column_name ) return; //Get number of slices from post meta $slices = get_post_meta($post_id, 'slices', true); echo intval($slices); }
I've stored the slices as metadata, but your columns can be populated by other data.
Now we register the custom column as "sortable". As mentioned above, we use themanage_{$screen->id}_sortable_column
filter.$screen->id
in this example is "edit-cake".
add_filter( 'manage_edit-cake_sortable_columns', 'my_sortable_cake_column' ); function my_sortable_cake_column( $columns ) { $columns['slices'] = 'slice'; //To make a column 'un-sortable' remove it from the array //unset($columns['date']); return $columns; }
$columns
The keys of the array represent sortable columns, and their values tell WordPress what to set 'orderby
' in queries. If the value is one of the 'orderby
's understood by WordPress itself (these values include 'title
', 'date
', 'modified
', 'comment_count
', or indeed any other value listed below for WP_Query in the WordPress Codex) We can stop here. The exceptions to this rule (as shown in this example) are the 'meta_value
' and 'meta_value_num
' parameters, which also require us to set a metakey.
If we want to sort by meta value, or by some other means that WordPress doesn't understand automatically, you have to tell it what sorting by "slice" means. If you order via post meta, the easiest way is to hook thepre_get_posts
action. This passes a query object that we can modify. Note that all default queries (front and back) trigger this action. While it's unlikely to cause any problems, unless you want WordPress to setorderby
to "slice" on the front-end as well, it's best to only affect the query on the admin side.
add_action( 'pre_get_posts', 'my_slice_orderby' ); function my_slice_orderby( $query ) { if( ! is_admin() ) return; $orderby = $query->get( 'orderby'); if( 'slice' == $orderby ) { $query->set('meta_key','slices'); $query->set('orderby','meta_value_num'); } }
This checks if our query is sorted by "slice" and if so, it tells WordPress to numerically sort by the value of the "slice" post meta. If you want to sort the values alphabetically, use 'meta_value
' instead of 'meta_value_num
'.
$query
is aWP_Query
object, so you can use that object to sort anything, and you can also sort columns. For other, more complex things, you'll want to hook theposts_orderby
(orpost_clauses
) hook, but that's beyond the scope of this quick tip.
NOTE:If a post does not store a value for that meta key, then the post will not display when you sort by that meta key. This is different from posts that store 0 as a meta value.
The above is the detailed content of Quick Tip: Enable Sortable Custom Columns. For more information, please follow other related articles on the PHP Chinese website!