[Gelöst] Post Form Categories: How to Only Display select Categories?
This support ticket is created vor 5 Jahren, 4 Monaten. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
Tell us what you are trying to do?
I would like to add a category field to my Post Form but only display certain categories. I initially tried hiding the unwanted ones with CSS, but that code is overwritten. Is there a way to designate which taxonomies should display? I would also like to add labels above the category groups so this would be useful to break them apart.
If I use the taxonomy field, they categories do not auto assign to the posts. I want the selection in the form to designate the category of the post.
Is there any documentation that you are following?
I've produced a fairly generic code sample which you can use to restrict what terms are included in a taxonomy field in a form, which can also be used for a taxonomy search filter in a View.
It uses the WordPress get_terms filter, and to avoid it applying everywhere you must specify the page(s) where the Form and/or Views are inserted, as well as providing the taxonomy slug and an array of term slugs to either blacklist or whitelist as required.
/**
* Blacklist or whitelist terms in a taxonomy form field or View filter
*/
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
// 1. pages where form (or search View) is inserted
$pages = array( 118, 137 );
// 2. which taxonomy (slug)
$taxonomy = 'status';
// 3. add terms to blacklist *OR* whitelist, not both
$blacklist = array();
$whitelist = array( 'archived', 'completed' );
if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {
if ( !empty( $blacklist ) ) {
foreach( $terms as $key => $term ){
if ( in_array( $term->slug, $blacklist ) ) {
unset($terms[$key]);
}
}
} elseif ( !empty( $whitelist ) ) {
foreach( $terms as $key => $term ){
if ( !in_array( $term->slug, $whitelist ) ) {
unset($terms[$key]);
}
}
}
}
return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );