Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
+1. Bugs me as well. Agree with you that your solution feels pretty hacky, but don't see any better options either. –  Johannes Pille Nov 28 '12 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 '12 at 16:19
    
    
Ah... completely missed that! A comment to another function confused me. Thanks. –  s_ha_dum Nov 28 '12 at 16:21
    
Yeah, I even looked at that source file. I just missed it 0_o –  s_ha_dum Nov 28 '12 at 16:28
show 1 more comment

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.

share|improve this answer
    
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 '12 at 17:16
    
True. Perhaps this is closer to what you need? Category Checklist Tree –  Chris Nov 28 '12 at 17:20
    
Oddly enough it uses the same fix that is used in @s_ha_dum answer –  Tom J Nowell Nov 28 '12 at 17:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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