How to make your custom columns sortable in WordPress?

How to make your custom columns sortable in WordPress?

In a recent article by Claudio Simeone, he demonstrated how you could add extra columns to your post, or custom post type, admin screens (or remove existing ones). In this quick tip I build on that by showing you how to make your newly created columns sortable. To tell WordPress which columns you want to register as sortable you need this filter:

How to sort a post type in Excel?

In general, for a post type with name ‘ my-post-type ‘, it is ‘ edit-my-post-type ‘. The filter passes an array with the names of sortable columns as keys, and what to order by as a value. More precisely, the values dictate what the ‘ orderby ‘ parameter is set as in the query that populates the table.

How to sort columns in Excel by name?

For posts and pages, $screen->id is ‘ edit-post ‘ and ‘ edit-page ‘ respectively. In general, for a post type with name ‘ my-post-type ‘, it is ‘ edit-my-post-type ‘. The filter passes an array with the names of sortable columns as keys, and what to order by as a value.

Which is the filter for my post type?

In general, for a post type with name ‘ my-post-type ‘, it is ‘ edit-my-post-type ‘. The filter passes an array with the names of sortable columns as keys, and what to order by as a value.

Is there a way to sort data in WordPress?

By default WordPress does a decent job of sorting the data, including taxonomies recently. However, for meta data (such as the ACF field we’ve added) it needs some guidance. To get things working as expected you’ll need to modify the query for the table data.

How to order custom posts in WordPress plugin?

Order posts (posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary. You can do directly on default WordPress administration. Excluding custom query which uses order or orderby parameters, in get_posts or query_posts and so on. This plugins is now supported and maintained by Colorlib.

How to sort by post meta in WordPress?

If we want to sort by a meta value, or by some other way WordPress doesn’t automatically understand you have to tell it what you mean by ordering by ‘slice’. If you are ordering by post meta, the easiest way to do this is to hook onto the pre_get_posts action. This passes a query object which we can modify.

Why do I need custom columns in WordPress?

Adding custom columns to the admin tables for your custom post types is a good way to provide more information to your users. WordPress provides a flexible method to add data whether it’s meta values, taxonomies, images, ACF PRO fields, or anything else tied to your post types.

Can you use meta box on front end post form?

There you have it. Now that you have the basics all figured out, you can make a front-end post form, you can allow image uploads, and now you can use meta boxes to gather and display all sorts of other information. Time to go make some sort of fancy post form. Let me know what you come up with!

What’s the best way to order by post meta?

If you are ordering by post meta, the easiest way to do this is to hook onto the pre_get_posts action. This passes a query object which we can modify. Note, that this action is fired for all default queries (front and back).