Discussion:
Child categories
Dobri
2014-01-23 14:09:02 UTC
Permalink
Hi all,

Say you have a custom hierarchical taxonomy (think categories). Would there be a way to limit term selection for that taxonomy to only children terms (level 2+)? I'm thinking something better than gray out the checkbox with javascript or filters, that seems like a hacky way to do it.

Any ideas? Thanks!

~Dobri
Nikola Nikolov
2014-01-23 14:11:43 UTC
Permalink
Custom meta box? If you don't want to do it with the standard meta box, I
think a custom one is the only other approach. You would obviously need to
make sure the input doesn't include a top-level category in case one of the
users is a smart one.

This would give you the best control over what the user can select and what
not.
Post by Dobri
Hi all,
Say you have a custom hierarchical taxonomy (think categories). Would
there be a way to limit term selection for that taxonomy to only children
terms (level 2+)? I'm thinking something better than gray out the checkbox
with javascript or filters, that seems like a hacky way to do it.
Any ideas? Thanks!
~Dobri
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dobri
2014-01-23 14:24:08 UTC
Permalink
Yeah,

Thanks, Nikola. That's kinda what I was trying to avoid but I guess there's no built-in way to do it.

I think I'll look into filtering the standard taxonomy meta box content instead of creating my own so if someone has an idea if there are any hooks for that, that'd be helpful.

~Dobri
Post by Nikola Nikolov
Custom meta box? If you don't want to do it with the standard meta box, I
think a custom one is the only other approach. You would obviously need to
make sure the input doesn't include a top-level category in case one of the
users is a smart one.
This would give you the best control over what the user can select and what
not.
Post by Dobri
Hi all,
Say you have a custom hierarchical taxonomy (think categories). Would
there be a way to limit term selection for that taxonomy to only children
terms (level 2+)? I'm thinking something better than gray out the checkbox
with javascript or filters, that seems like a hacky way to do it.
Any ideas? Thanks!
~Dobri
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Jeremy Clarke
2014-01-30 15:29:02 UTC
Permalink
Post by Dobri
I think I'll look into filtering the standard taxonomy meta box content
instead of creating my own so if someone has an idea if there are any hooks
for that, that'd be helpful.
Based on past experiences the best/only place to control the output of the
categories box is actually pretty far down in the chain: get_terms()

Here's the filter and first line of the function I use (which
re-alphabetizes the list because I had an issue with Arabic text coming out
not in Arabic-alphabetical-order):

add_filter('get_terms', 'gv_filter_get_terms', 10, 3);
function gv_filter_get_terms($terms, $taxonomies, $args) {
[...]
}

The main problem you'll have using this method is the need to very
explicitly avoid running your filter for cases other than the metabox. One
thing I found vital for this was testing to make sure that 'all' was set as
the 'fields' to retrieve:

if ('all' != $args['fields'])
return $terms;

If you don't have that check your code will choke when it does an 'ids'
query and your object-expecting code filters it into oblivion.

Since your code would probably be a lot more destructive than mine you
might want to consider more drastic measures, like inspecting the backtrace
chain (wp_debug_backtrace_summary()) for explicit confirmation that you're
filtering the content of the metabox.

Good luck,
--
Jeremy Clarke • jeremyclarke.org
Code and Design • globalvoicesonline.org
Loading...