Last active
March 17, 2024 11:55
-
-
Save alexphelps/d3488fd7aae7f82196535bf1d71719b7 to your computer and use it in GitHub Desktop.
WooCommerce Products Filter Custom Taxonomy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Filter team products from main shop only on frontend on shop, category, and tag archives | |
*/ | |
function exclude_team_products( $query ) { | |
$archive_query = $query->is_post_type_archive('product') && $query->is_main_query(); | |
$cat_tag_query = $query->is_tax( array('product_cat', 'product_tag') ) && $query->is_main_query(); | |
if ( !is_admin() && $archive_query || !is_admin() && $cat_tag_query ) { | |
$taxquery = array( | |
array( | |
'taxonomy' => 'product_team', | |
'field' => 'id', | |
'terms' => '', //leave blank to filter all teams | |
'operator'=> 'NOT EXISTS' | |
) | |
); | |
$query->set( 'tax_query', $taxquery ); | |
} | |
} | |
add_action('pre_get_posts', 'exclude_team_products' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment