Skip Navigation

[Closed] Filtering by child categories only

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

Assisted by: Christian Cox.

Author
Posts
#1688097

We're setting up a filter on displaying posts and would like it to use the child categories of a specific category *only*. Is there a way to specify this?

#1689015

Hi, there's not an easy way to specify this in the View Builder or Block Editor, but you can use some custom PHP to whitelist or blacklist any specific terms from the filter options list. I have a whitelist example you can customize for your needs:

/**
 * 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 displayed
    $pages = array( 123, 456 );

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

- Change 123, 456 to be a comma-separated list of Page IDs where you are showing the custom search View.
- Change status to be the slug of the taxonomy being filtered.
- Update the whitelist array to include a comma-separated list of term slugs for all the terms you want to include in the filter options. Enclose each term slug in single quotation marks.
- You should not edit anything below the whitelist array variable declaration.
- Place the updated code in your child theme's functions.php file, or add the code to a new custom code snippet in Toolset > Settings > Custom Code.

There are some limitations to this approach you should be aware of:
- You must manage the whitelisted or blacklisted term list manually. For a more dynamic implementation, you may implement the WP_Term_Query API to fetch child terms and build your whitelist or blacklist programmatically. More information about that API is available in the WP documentation: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
- This solution works in basic search View implementations and is not intended for use with advanced search features, such as "Only show available options for each input".

Let me know if you have questions about implementing this custom code.

The topic ‘[Closed] Filtering by child categories only’ is closed to new replies.