How to keep hierarchy in post category checklists

Posted by: jared

If you find yourself in a situation where you have many categories that have repeating sub categories you may end up with a very disorganized Categories metabox when you are creating/editing a post. This makes it difficult to determine which category you are actually assigning to a post.

unnested-categories

The problem here is that WordPress by default tries to make things easier for the user by putting checked categories at the top. Unfortunately this breaks the nesting of categories. Don’t worry there’s a filter for that.


/**
*  Fix category checklist sorting
*/
function cab_terms_checklist_args( $args, $post_id ) {

if ( isset( $args['taxonomy'] ) && 'category' == $args['taxonomy'] ) {

$args['checked_ontop'] = false;

}

return $args;

}

add_filter( 'wp_terms_checklist_args', 'cab_terms_checklist_args' );

 

After adding this filter your categories will display in their proper hierarchical order.

nested-categories

Comments are closed, but trackbacks and pingbacks are open.