4

The objective is to ensure that when editing a post, the metabox that lists a hierarchical taxonomy doesn't reorder itself upon Update to put checked items on top.

Right now, edit-form-advanced.php calls add_meta_box(), passing a callback of post_categories_meta_box, which is defined in meta-boxes.php.

post_categories_meta_box() invokes wp_terms_checklist(), leaving the checked_ontop parameter undefined. wp_terms_checklist() defaults that parameter to true, which is the root cause of all this hooplah.

Nary a filter hook to be found anywhere along the chain until we back out and discover add_meta_boxes and add_meta_boxes_{$post_type}.

The only solution I was able to come up with myself was to hook onto one of these actions, globalize $wp_meta_boxes, and replace occurrences of the post_categories_meta_box callback with a callback of my own, where I completely duplicate all of post_categories_meta_box() and simply add the parameter 'checked_ontop' => false.

Works. But fugly. This seems ridiculous - is there no more elegant way to simply let your hierarchical taxonomies keep their order in metaboxes?

2 Answers 2

2

There's a plugin written specifically to fix this 'feature' of WordPress, it's called Category Checklist Tree. Despite the name, it works on custom taxonomies too.

2
  • 1
    Scribu's plugin, as Admirable as it is, does exactly the same thing: it duplicates the entire function, and then modifies the argument, As I described. I probably was not as clear in my original post as I should have been: the request was to find a more elegant way of achieving the objective, not for a plug-in that would help me do it, since I had already rolled my own code to accomplish that. But thanks for the link to this plug-in! There is a ton of good stuff in there: I always learn something new when I read through Scribu's code.
    – Tom Auger
    Commented Mar 21, 2012 at 23:36
  • Your way (and scribu's) is the way to do it. It's not fugly, it's just how it is. That's why the WP core uses its own API (ala add_meta_box): so you can replace its functionality with your own. The alternative is probably an even uglier javascript hack. Commented Jul 6, 2012 at 1:09
0

Nowadays there is a hook available for this problem: https://developer.wordpress.org/reference/hooks/wp_terms_checklist_args/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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