Dieser kurze Beitrag zeigt, wie man in WooCommerce Kategorien auf Shop-Seiten und in der Produkt-Suche verstecken kann. Wir benutzen dazu das folgende Code Snippet:
add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 ); add_action( 'pre_get_posts', 'remove_categories_from_search_results',99 ); function ts_get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if it is a product category and on the shop page if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) { foreach( $terms as $key => $term ) { if ( !in_array( $term->slug, array( 'uncategorised','unkategorisiert' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; } function remove_categories_from_search_results( $query ) { if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { $query->set( 'post_type', array( 'product' ) ); $tax_query = array( array( // likely what you are after 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array('uncategorised', 'unkategorisiert'), 'operator' => 'NOT IN', ), ); $query->set( 'tax_query', $tax_query ); } }
Die zu versteckenden WooCommerce-Kategorien müssen den Arrays in Zeile 9 und 30 hinzugefügt werden.