The problem
If you’ve created a custom post type, and enabled the post_attributes meta box, to allow users to adjust the ordering of the posts within your post type, you may expect the WordPress Admin Edit screen to list the posts by the menu order you’ve set, as per the Edit Pages screen.
However, instead, it continues to display the menu items by date order, with most recent first.
How do you order your post type Edit screen?
The solution
Setting up your Custom Post Type
Let’s assume you’ve set up your Custom Post Type as follows:
-
// Create Portfolio Custom Post Type
-
function create_post_type() {
-
register_post_type( 'portfolio',
-
array(
-
'labels' => array(
-
'name' => 'Portfolio',
-
),
-
'public' => true,
-
'menu_position' => 5,
-
'rewrite' => array('slug' => 'portfolio'),
-
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
-
)
-
);
-
}
-
-
add_action( 'init', 'create_post_type', 11);
Here we’ve created a Portfolio custom post type. For more information on custom post types, see WordPress Codex: register_post_type()
Note that the ‘supports’ parameter includes ‘page-attributes’ – this allows you to display the menu order box on your edit screen.
Changing the admin screen ordering
The Portfolio custom menu screen will currently be displaying by date. ‘Portfolio’ by nature may well be ordered not by date but by menu order.
To change the ordering, we need to change the query that pulls in the custom posts for the Portfolio Edit screen. We use the ‘pre_get_posts’ filter to add parameters to the query.
-
function posts_for_current_author($query) {
-
-
if($query->is_admin) {
-
-
if ($query->get('post_type') == 'portfolio')
-
{
-
$query->set('orderby', 'menu_order');
-
$query->set('order', 'ASC');
-
}
-
}
-
return $query;
-
}
-
add_filter('pre_get_posts', 'posts_for_current_author');
Here we are first checking if it’s an admin screen query. Be aware that the ‘pre_get_posts’ filter is called for all post queries, public-facing or admin-facing, so you need to narrow it down to the specific query that you want to adjust.
Then we add two parameters to the query:
1) Set the ‘orderby’ parameter to ‘menu_order’
2) Set the ‘order’ parameter to ‘ASC’
The ‘orderby’ parameter changes the order – there are many different parameters, see WordPress Codex: WP_Query#Parameters. Here we’re changing it to ‘menu_order’ so that we’re ordering the query by the menu order field.
Changing the ‘order’ parameter to ASC (ascending) means that 0 comes first, then 1, then 2, etc. Otherwise it would display in reverse order by default.
Solved
And there you have it. If this has helped you, please leave me some feedback so that others can see that this is useful information.
[...] How to order your custom post type edit screen by ‘menu order’ | Dan Smart – Web Developer I have worked in the software development industry for over 10 years, with experience in web development, mobile handset development, and mobile networks. My current focus is web development, working freelance with a number of clients, working both on design and implementation of websites and web applications with systems such as WordPress, Joomla, and CodeIgniter. When I’m not developing websites and software, I am a keen runner, involved with mime performance group Innovo Physical Theatre, and also actively involved in my local church. [...]
Thank you for this information. It was very helpful. I could apply it to a slide menu plugin. Great job
Thank you! Very useful info!