Prevent checked categories in meta box from appearing at top in WordPress

I previously shared a jQuery snippet that enforces parent checkboxes to be checked when a child is checked and children to be unchecked if the parent is unchecked.

This works really great in theory, except that WordPress, by default, moves all checked checkboxes to the top of the category list, which effectively breaks the JavaScript snippet because it depends on the hierarchy.

If enforcing the checkboxes is more important to you than how they are visually displayed, here’s a WordPress filter that lets you prevent checked checkboxes from moving to the top.

	NS . 'wp_terms_checklist_args'

function wp_terms_checklist_args($args) {
	$args['checked_ontop'] = false;
	return $args;

The constant NS is what I use to maintain namespaces in the plugins I write. This ensures compatibility without having to write crazy long function names or create a class just for the sake of namespacing.

4 thoughts on “Prevent checked categories in meta box from appearing at top in WordPress

  1. Attila

    thanks for this!
    I have several child categories with the same name, so it was really confusing when they moved on top.
    Strange feature.. anyway thanks!

  2. makinero

    I think the code is buggy.

    I had to replace this :

    NS . 'wp_terms_checklist_args'

    by this :

    'wp_terms_checklist_args' (removing the NS.) in order to make it work.

    1. Ryan Sechrest Post author

      You’re right, if you don’t set it up how I explained it in the linked article above, your code works just as well. I can see how that would be confusing.


Leave a Reply

Your email address will not be published.