3

When you're editing a post and select a child category or term, and update, the selected terms appear at the top ( leading to potentially confusing situations where 'foo' 'foo' and 'foo' are selected each with different parents and no way to tell them apart ).

How do I prevent this behaviour? Either by showing the parent terms to give context, or stopping them being moved to the top

2 Answers 2

4

This bugs me too. It is very confusing, so I thought I'd look into it.

That meta box is created in "wp-admin/includes/meta-boxes.php" on line ~399. It uses wp_terms_checklist. The problem codes seems to be this (it is one in source):

<?php wp_terms_checklist(
    $post->ID, array( 
        'taxonomy' => $taxonomy, 
        'popular_cats' => $popular_ids 
) ) ?>

That leaves a couple of options that I can think of.

You could use Javascript to reorder the list. You can't remove the oddball categories because they won't be available in the list. But I doubt that would be either quick or easy.

Or, you can remove the default box with remove_meta_box and add back a near clone with add_meta_box altering the line above to be:

<?php wp_terms_checklist(
    $post->ID, 
    array( 
        'taxonomy' => $taxonomy,
        'popular_cats' => $popular_ids 
        'check_ontop' => false 
) ) ?>

The checked_ontop part is the key. Removing and adding a metabox seems a long way to go to add those few characters and I sort-of hate to do it because it is pretty close to a core hack (but isn't :) ), though changes to the core could mean a need to rewrite that function. I don't think there is any other real option though.

EDIT: OK. I originally thought that there was no hook that would help with this but there is one. All you need is:

function tst($args) {
    $args['checked_ontop'] = false;
    return $args;
}
add_filter('wp_terms_checklist_args','tst');

That is going to alter things globally so it may be necessary to alter that so that it only runs on particular pages.

Thanks to Tom J Nowell. Sometimes it is good to be wrong.

7
  • +1. Bugs me as well. Agree with you that your solution feels pretty hacky, but don't see any better options either. Nov 28, 2012 at 16:13
  • Ah but that's not entirely true, the array passed into the function isn't passed into a filter, but thats because the first thing wp_terms_checklist does is pass it into a filter
    – Tom J Nowell
    Nov 28, 2012 at 16:19
  • Ah... completely missed that! A comment to another function confused me. Thanks.
    – s_ha_dum
    Nov 28, 2012 at 16:21
  • Yeah, I even looked at that source file. I just missed it 0_o
    – s_ha_dum
    Nov 28, 2012 at 16:28
0

I might be misunderstanding the question, but if you know, looking at the initial hierarchy, which subcategories are appropriate, then you could use a plugin like Parent Category Toggler to auto-select the parent when you select the sub.

3
  • That would ~semi-solve the problem by applying all the terms ancestors to the post, but it doesn't prevent the behaviour, and doing that has other implications ( you dont need to check the parents for the post to appear in the parent terms anyway). It's also an extra step, any solution should be automated/transparent/effortless
    – Tom J Nowell
    Nov 28, 2012 at 17:16
  • True. Perhaps this is closer to what you need? Category Checklist Tree
    – Chris
    Nov 28, 2012 at 17:20
  • Oddly enough it uses the same fix that is used in @s_ha_dum answer
    – Tom J Nowell
    Nov 28, 2012 at 17:56

Your Answer

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

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