17

Set WordPress custom post types order in admin

Posted June 19th, 2010 in WordPress by Mark Leong

One of the long-awaited new features of WordPress 3.0 is custom post types.

By default, custom posts with the post capability_type (as opposed to page, attachment, revision, or nav-menu-item) are listed newest to oldest in the WordPress backend. This makes sense if these are blog posts, but if they are some other content type (such as quotes, movies, books or contacts) it is often desirable to have them listed alphabetically.

To achieve this, add the following code to your functions.php file, replacing POST_TYPE (line 7) with the name of the post type that you want to have listed alphabetically.

function set_custom_post_types_admin_order($wp_query) {
  if (is_admin()) {

    // Get the post type from the query
    $post_type = $wp_query->query['post_type'];

    if ( $post_type == 'POST_TYPE') {

      // 'orderby' value can be any column name
      $wp_query->set('orderby', 'title');

      // 'order' value can be ASC or DESC
      $wp_query->set('order', 'ASC');
    }
  }
}
add_filter('pre_get_posts', 'set_custom_post_types_admin_order');

17 Responses so far.

  1. Question: does this mean it is now possible in WordPress to have a “custom post type” of “link”, like I currently have on my website? (In other words, clicking on the title goes to another website, rather than my own, and is styled differently.) If so, and I have a free couple of days, I might switch to WordPress as I keep on having to hack Chyrp about to fix it and development just halted on future releases!

    • Mark Leong says:

      Hi Matt,

      Using Custom Fields (http://codex.wordpress.org/Custom_Fields) and a little template file tweaking it should be possible to do what you want. Here’s how I’d go about it:

      1. On Posts that I wanted to link to an external URL, I’d add a Custom Field, calling it something like ‘external-link’ and giving it the value of the external URL.

      2. Edit the WordPress Loop so that it checks to see if the ‘external-link’ custom field is set. If it isn’t set, display the post title with the internal link as usual. If it is set, link to that external link instead. The Loop appears in most WordPress template files (index.php, single.php, archive.php etc.), so you’d need to check through them to make sure you changed them all. I’m assuming you’ve come across the WordPress Loop before; if not, here’s the documentation page http://codex.wordpress.org/The_Loop.

      As an alternative to using a Custom Field, you could add a meta box (like the author box) to the Post admin interface using add_meta_box (http://codex.wordpress.org/Function_Reference/add_meta_box). This would look a bit smarter I reckon, although functionally it does the same thing.

      If you try this but get stuck on tweaking the Loop output in the template files let me know and I should be able to point you in the right direction.

  2. Mark,

    Thanks for this… Any idea how to get your custom post type to be editable order (like Pages are)?

    That would come in handy. I am happy to have custom post types, but there isn’t a lot of documentation out there yet.

    Thanks

  3. nsp-code says:

    Maybe you should just try the Post Types Order plugin? something like what PostMash does but for all post custom types including posts and pages

  4. Thibaut says:

    In fact your could just set ‘hierarchical’ to “true” in the register_post_type declaration.

  5. Sam says:

    Thanks, Mark! This is exactly what I was looking for. It seems like such a common need for custom post types I’m surprised there’s no option to set this during the registration process.

  6. Wes says:

    Thanks for this, it was just what I was looking for.

    I’d like to add some code to solve a problem I had:

    Add !isset($_GET['orderby']) before the set function to prevent the filter from overriding the sort order when you explicitly ask (eg. clicking on the post author or post date).

    My function then becomes

    function app_list_admin_order($query) {
    if (is_admin()) {
    $post_type = $query->query['post_type'];
    if ($post_type = 'custom_post_type') {
    if (!isset($_GET['orderby'])) {
    $query->set('orderby', 'title');
    $query->set('order', 'asc');
    }
    }
    }
    }

  7. JL says:

    Thanks man!

  8. Simo says:

    It doesn’t work for me, and i need it ASAP

    my post type name : clavebloc (‘desc-project’,'date-project’)
    and i want to order them by ‘date-project’ column
    i replaced it but nothing happend

    I Need Help please !!

  9. Simo says:

    function set_custom_post_types_admin_order($wp_query) {
    if (is_admin()) {

    // Get the post type from the query
    $post_type = $wp_query->query['post_type'];

    if ( $post_type == ‘clavebloc’) {

    // ‘orderby’ value can be any column name
    $wp_query->set(‘orderby’, ‘date-projet’);

    // ‘order’ value can be ASC or DESC
    $wp_query->set(‘order’, ‘ASC’);
    }
    }
    }
    add_filter(‘pre_get_posts’, ‘set_custom_post_types_admin_order’);

  10. Pranjal says:

    Thanks, for the solution. Though, it wasn’t exactly what I wanted to know, but clearly give me an idea on how to fix my issue.

    Thanks again! :)

  11. Vilius says:

    Thanks! Exactly what I needed ; )

  12. home says:

    Hello, Neat post. There is an issue along with your site in internet explorer, may test
    this? IE still is the market chief and a good element of other folks will pass over your fantastic writing due to this problem.

Leave a Reply