I would like to know if it is possible in Wordpress admin to show categories in post-new.php and edit-tags.php?taxonomy=category sorted by id NOT by category name.

link|improve this question
feedback

1 Answer

The following should work...

add_action('get_terms_args','my_order_cats',10,2);
function my_order_cats($args,$taxonomies){
    //Check we are admin side
    if(is_admin()){
        $taxonomy = $taxonomies[0]; 
        $screen = get_current_screen();
        //Check screen ID and taxonomy and changes $args where appropriate. 
        if(($screen->id=='edit-category'||$screen->id=='post') && $taxonomy=='category'){
            $args['orderby']='id'; //preserves order of subcategories.
            $args['order']='asc'; //or desc
        }
    }
    return $args;
}

It preserves the order of subcategories (i.e. children always appear below their parents regarldless if order is set to ASC/DESC).

This could be adapted for custom taxonomies, you would simply need to chhange the $screen->ID and $taxonomy checks.

link|improve this answer
I think I would do the if( is_admin() ) on the add_action()...it seems slightly more efficient, just a thought. Good solution though :) – m0r7if3r Feb 19 at 2:58
I tried the above solution and it wordks in the taxonomy page. But it doesn't work in the add new post page, and the category box at the right sidebar. Any ideas? – Satsilem Feb 20 at 9:14
Sorry missed that! All you need to check the screen ID. I'll update my answer. However, it would work for new posts and editing old posts - is that a problem? You can probably make it apply to only new posts though... – Stephen Harris Feb 20 at 10:17
@m0r7if3r yeah probably, but I've always kept the logic inside hooked functions - aesthetics more than anything :D. – Stephen Harris Feb 20 at 10:21
@StephenHarris I'm pretty concerned with performance...probably too concerned with performance at times :D – m0r7if3r Feb 20 at 16:01
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.