When making custom post types, or even merely adding a custom column to posts or pages, being able to sort the list of posts or pages in order of that column can be important to a client (or yourself).
Scribu has a seemingly handy tutorial to guide you on your way. Or do they?
If you are like me and this tutorial produced no results, in this article is a good hack to get the job done.
In several cases, clients have requested that we provide them the ability to show data in a certain order within the WordPress Dashboard. An order they want to control. So we provide them some custom meta boxes in the post type called “Display Order.” In the WordPress Dashboard, we want to allow them to display these items in that order. Thankfully WordPress 3.1 provides such an ability; for Title, Date and Comments.
So, in today’s example, we are going to be making a sortable column for a custom field in a post type labeled “banners” (for rotating banners). The client wants them to display in a custom order that they can alter at will.
The first step is to make the custom column:
In 3.0.x it is likely that creation of a column title like this would just be a string, no markup involved. This is simply to emulate the existing sortable columns. Secondly, we need to show the data:
Nothing different about this.
This I put in a file named something like “banner-custom.js”. Then you need to initialize the with enqueue_script so that the script is properly loaded in the admin area:
There you are, a sortable column. You can repeat this with any column, be it a string or a number.
NOTE: Seeing as the order are numbers, you must use “orderby=meta_value_num” in the query string for this to not produce ‘1, 10,11, 12, 2, 3, 4’, for example. To sort strings by alphabetical order, simply replace ‘meta_value_num’ with ‘meta_value’.