Skip Navigation

[Resolved] Post Form Categories: How to Only Display select Categories?

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.

Our next available supporter will start replying to tickets in about 6.47 hours from now. Thank you for your understanding.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 1 year, 8 months ago.

Assigned support staff: Nigel.

Author
Posts
#1297391

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?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1298523

Nigel
Supporter

Languages: English (English ) Spanish (EspaƱol )

Timezone: Europe/London (GMT+01:00)

Hi Lilian

I only just had time to get to this this morning.

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 );