Sort Custom Post Types in WordPress Admin

» Posted by on Jun 26, 2011 in Blog, Web Development, Wordpress | 5 comments

My favorite addition to WordPress 3 is the ability to easily add custom post types. I’ve utilized this feature in nearly all my new projects, and now I’m not quite sure what I ever did without them. One thing that wasn’t included in their release was the ability to easily sort these post types in the WordPress admin. By default the admin sorts them alphabetical by title, which isn’t ideal for most situations. The easiest way to achieve this without using one of the various sorting plugins out there is by applying a filter to WordPress’ parse_query hook.

You can sort by all of the typical WordPress query variables. Below is an example to sort your custom posts in reverse chronological order by post date, as you’re accustomed to with WordPress’ default Post type.

add_filter( 'parse_query', 'order_by_query' );
function order_by_query( $query ) {
    global $pagenow;
    if ( is_admin() && $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && isset( $_GET['post_type'] ) == 'your_post_type' && isset( $wp_query->query['orderby'] )  ) {
        $query->query_vars['orderby'] = 'date';
        $query->query_vars['order'] = 'DESC';
    }
}

Simply add the above filter to your themes functions.php file, plugin, or alongside wherever you register your custom post type.

 

Edit: As Bruce noted below in the original example I neglected to include making the list actively sortable. The above function now includes this. Thanks, Bruce.

Related posts:

  1. Remove WordPress 3.1 Admin Bar

5 Comments

  1. This doesn’t work with sortable columns. You need to add `!isset($_GET['orderby'])`.

  2. Thanks, Bruce. I’ve update the function.

  3. Hey, I came across your answer in a google search for a solution to this problem, but this doesn’t seem to actually be making any adjustments to my admin page at all. I’ve tried several values to sort by, and no changes happen. Also, my custom post type is sorted, by default, by ID, not alphabetically by title (which is actually what I prefer) Do you have any idea why it may not be working?

    • Quentin,

      This works, but the update Chris made excludes the !. It should look like:

      `add_filter( ‘parse_query’, ‘order_by_query’ );
      function order_by_query( $query ) {
      global $pagenow;
      if ( is_admin() && $pagenow == ‘edit.php’ && isset( $_GET['post_type'] ) && isset( $_GET['post_type'] ) == ‘your_post_type’ && !isset( $wp_query->query['orderby'] ) ) {
      $query->query_vars['orderby'] = ‘date’;
      $query->query_vars['order'] = ‘DESC’;
      }
      }`

      • Actually, let’s make that:

        add_filter( ‘parse_query’, ‘order_by_query’ );
        function order_by_query( $query ) {
        global $pagenow;
        if ( is_admin() && $pagenow == ‘edit.php’ && isset( $_GET['post_type'] ) && isset( $_GET['post_type'] ) == ‘your_post_type’ && !isset( $_GET['orderby'] ) ) {
        $query->query_vars['orderby'] = ‘date’;
        $query->query_vars['order'] = ‘DESC’;
        }
        }

Submit a Comment