Better way to enforce category hierarchy in post_categories_metabox?


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?

Tom Auger 2 years 2020-12-14T16:10:22-05:00 0 Answers 8 views 0

Leave an answer