Ordering posts by custom taxonomy in admin area

EDIT

@kaiser – this question is slightly different to the answer you posted in that I am talking about sorting by clicking the column title rather than adding a dropdown to filter by taxonomy term (which you’ll see I’ve already done in the screenshot).

Read More

Like Otto says in the answer Milo linked to: ‘it is not possible to order by taxonomy, because from a certain type of standpoint, that doesn’t actually make much sense…the point of having a taxonomy on posts would really be to have terms in that taxonomy that are shared between posts’.

I see his point, as you can have multiple terms attached to a post, sorting by them is a bit pointless. I’ve decided to scrap the idea, so feel free to delete this post if it’s of no use. Thanks.

ENDEDIT

Been scouring the net for the solution to this, but not finding the specific answer to my issue. I’ve registered a custom post type and a couple of custom taxonomies, but I’m trying to allow admins to sort posts by a taxonomy, just like you can with the Date column – see screenshot here:

enter image description here

Does anyone know how to do that? Here’s how I’m registering the custom post type if it’s of any use:

Class OSUFestivals
    {
        public static function init()
        {
            add_action( "init", array( __CLASS__, "register_directory_cpt" ) );
            add_action( "init", array( __CLASS__, "register_workshop_type_taxonomy" ) );
            add_action( "init", array( __CLASS__, "register_day_taxonomy" ) );
        }

        public static function register_directory_cpt()
        {
            $labels = array(
                'name'               => 'Festival Items',
                'singular_name'      => 'Festival Item',
                'menu_name'          => 'Festival',
                'name_admin_bar'     => 'Festival Item',
                'add_new'            => 'Add New Item',
                'add_new_item'       => 'Add New Item',
                'new_item'           => 'New Item',
                'edit_item'          => 'Edit Item',
                'view_item'          => 'View Item',
                'all_items'          => 'All Items',
                'search_items'       => 'Search Items',
                'parent_item_colon'  => 'Parent Items:',
                'not_found'          => 'No Items found.',
                'not_found_in_trash' => 'No Items found in Trash.',
            );
            $args = array(
                'labels'        => $labels,
                'public'        => true,
                'hierarchical'  => false,
                'has_archive'   => true,
                'show_in_admin_bar' => true,
                'menu_position' => 5,
                'supports'      => array( 'title', 'editor', 'thumbnail', 'author', 'excerpt', 'revisions', 'page-attributes' ),
                'rewrite'       => array(
                    'with_front'    => false,
                    'slug'          => 'festival-Items'
                )
            );

            register_post_type( 'lkfestival', $args );
        }

        public static function register_workshop_type_taxonomy()
        {
            $labels = array(
                'name'                      => 'Workshop types',
                'singular_name'             => 'Workshop type',
                'search_items'              => 'Search workshop types',
                'all_items'                 => 'All workshop types',
                'parent_item'               => 'Parent workshop type',
                'edit_item'                 => 'Edit workshop type',
                'update_item'               => 'Update workshop type',
                'add_new_item'              => 'Add new workshop type',
                'new_item_name'             => 'New workshop type',
                'choose_from_most_used'     => 'Choose from most used workshop types'
            );

            $args = array(
                'hierarchical'      => true,
                'labels'            => $labels,
                'show_admin_column' => true,
                'rewrite'           => array( 'slug' => 'event-type' ),
                'capabilities'      => array (
                    /* Only let Andrea assign workshop types */
                    'manage_terms'  => 'manage_options', // by default only admin
                    'edit_terms'    => 'manage_options',
                    'delete_terms'  => 'manage_options',
                    'assign_terms'  => 'edit_posts'  // means administrator', 'editor', 'author', 'contributor'
                )
            );

            register_taxonomy( 'lktypes', 'lkfestival', $args );
        }

        public static function register_day_taxonomy()
        {
            $labels = array(
                'name'                      => 'Festival days',
                'singular_name'             => 'Festival day',
                'search_items'              => 'Search festival days',
                'all_items'                 => 'All festival days',
                'parent_item'               => 'Parent festival day',
                'edit_item'                 => 'Edit festival day',
                'update_item'               => 'Update festival day',
                'add_new_item'              => 'Add new festival day',
                'new_item_name'             => 'New festival day',
                'choose_from_most_used'     => 'Choose from most used festival days'
            );

            $args = array(
                'hierarchical'      => true,
                'labels'            => $labels,
                'show_admin_column' => true,
                'rewrite'           => array( 'slug' => 'day' ),
                'capabilities'      => array (
                    /* Only let Andrea assign festival days */
                    'manage_terms'  => 'manage_options', // by default only admin
                    'edit_terms'    => 'manage_options',
                    'delete_terms'  => 'manage_options',
                    'assign_terms'  => 'edit_posts'  // means administrator', 'editor', 'author', 'contributor'
                )
            );

            register_taxonomy( 'lkdays', 'lkfestival', $args );
        }

    }

    // Load Class
    OSUFestivals::init();

I thought the ‘sort’ parameter in register_taxonomy() would allow sorting, but it looks like that’s related to the order in which terms are added to objects.

Related posts

Leave a Reply

1 comment

  1. Here is what I do to sort my custom post type by custom taxonomy in the admin. I have a custom post type called plugin_filter and a custom taxonomy assigned to it called filter_group. I add the following filter to my plugin to make the field sortable.

    add_filter("manage_edit-plugin_filter_sortable_columns", 'plugin_filter_sort');
    function plugin_filter_sort($columns) {
       $custom = array(
           'taxonomy-filter_group' => 'taxonomy-filter_group'
       );
       return wp_parse_args($custom, $columns);
    }