Forked from alexphelps/wc-products-filter-custom-tax.php
Created
January 4, 2018 13:00
-
-
Save istvankrucsanyica/1e9134a9c9ac58abfc42a227a736946c to your computer and use it in GitHub Desktop.
WooCommerce Products Filter Custom Taxonomy
This file contains hidden or 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