Skip Navigation

[Résolu] Post Form Categories: How to Only Display select Categories?

This support ticket is created Il y a 4 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 1 réponse, has 2 voix.

Last updated by Nigel Il y a 4 années et 9 mois.

Assisted by: Nigel.

Auteur
Publications
#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: Anglais (English ) Espagnol (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 );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.