Skip Navigation

[Closed] Apply several query filters on same taxonomy

This support ticket is created 4 years, 9 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1577129

Hello,
I have a view with a parametric search filter based on a select filled with terms.
But I need to limit allowed checkboxes only to some of those terms, based on a custom term field.
Is there a built-in way to do this, something that I didn't understand ? Maybe thanks to nested views ?
Thank you.

#1577677

I can't think of a good way to do this with nested Views. There is one way I know of to limit a list of term filters using custom code to whitelist or blacklist certain terms via the get_terms hook. Here is an example where you can specify the page(s) where the View is shown, the slug of the taxonomy, and the slugs of the terms to whitelist or blacklist 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 example requires you to provide the term slugs for whitelisted OR blacklisted terms. You mentioned enabling or disabling certain terms based on a custom term field, but I don't have an example exactly like that. Using the example above, you can see how to use the get_terms hook to create an array of terms you want to include or exclude. You must add logic in here based on custom term field values using get_term_meta or types_render_termmeta. Let me know if you need more specific examples of get_term_meta or types_render_termmeta that are not included in the examples below:
https://toolset.com/documentation/user-guides/custom-content/displaying-wordpress-term-fields/
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://developer.wordpress.org/reference/functions/get_term_meta/

Note that this approach has limitations. One limitation is it doesn't work well with AJAX filter updates or "Only show available options for each input" in Views.

The topic ‘[Closed] Apply several query filters on same taxonomy’ is closed to new replies.