How to order your custom post type edit screen by ‘menu order’

September 30th, 2011


About the Author: Dan

I’m Dan Smart, a 38 year old website developer, based in Swansea, UK. I have worked in the software development industry for over 15 years, with experience in web development, mobile handset development, and mobile networks. I work both on websites and web applications with systems such as Wordpress, Laravel, Backbone, Angular.js, node.js and mobile app development with PhoneGap, iOS, and Android. 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.

* Follow me on Twitter or contact me


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:

  1. // Create Portfolio Custom Post Type
  2. function create_post_type() {
  3.    register_post_type( 'portfolio',
  4.     array(
  5.      'labels' => array(
  6.      'name' => 'Portfolio',
  7.      ),
  8.     'public' => true,
  9.     'menu_position' => 5,
  10.     'rewrite' => array('slug' => 'portfolio'),
  11.     'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
  12.     )
  13.    );
  14.   }
  15.     
  16.     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.

  1. function posts_for_current_author($query) {
  2.  
  3.   if($query->is_admin) {
  4.  
  5.         if ($query->get('post_type') == 'portfolio')
  6.         {
  7.           $query->set('orderby', 'menu_order');
  8.           $query->set('order', 'ASC');
  9.         }
  10.   }
  11.   return $query;
  12. }
  13. 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.


Switch to mobile version