GET FREE VERSION GET THE PRO VERSION

How To Search Only For Top Level Taxonomies

Learn how to force plugin to search only for top level product taxonomies.

In this article

Overview

Advanced Woo Search plugin has great feature to search and display products taxonomies.

With the free plugin version you can search and display product categories and tags.

With the PRO plugin version you can search for any WooCommerce product taxonomies and attributes.

When searching for any such taxonomy plugin will show in search results both parent taxonomy and all child taxonomies ( if all these taxonomies contain search terms ).

Product categories search results with parent and child terms

Product categories search results with parent and child terms

In this article we will cover the question - how to limit search results only for top level taxonomies - those that don't have any parent taxonomy terms.

Product categories search results with only top level terms

Product categories search results with only top level terms

Search only for top level taxonomies

To limit taxonomies search only to top level terms you just need to use the following code snippet:

add_filter('aws_terms_search_query', 'my_aws_terms_search_query', 10, 3);
function my_aws_terms_search_query( $sql, $taxonomy, $search_query ) {
    global $wpdb;
    $only_parent = "AND $wpdb->term_taxonomy.parent = 0";
    $sql = str_replace( 'WHERE 1 = 1', 'WHERE 1 = 1 ' . $only_parent, $sql);
    return $sql;
}

You need to add it somewhere outside the plugins folder. For example, inside functions.php file of your theme or use some plugin for adding code snippets.

Also, after adding this code you will need to go to the plugin settings page and click the Clear cache button.