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.

1
2
3
4
5
6
7
8
9
add_filter(
    'wp_terms_checklist_args',
    NS . 'wp_terms_checklist_args'
);
 
function wp_terms_checklist_args($args) {
    $args['checked_ontop'] = false;
    return $args;
}
add_filter(
	'wp_terms_checklist_args',
	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.

One thought on “Prevent checked categories in meta box from appearing at top in WordPress

  1. Attila

    Hello,
    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!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *