Created
December 6, 2017 16:52
-
-
Save lukecav/f638dabb43db94e360b043f5f66bfbc6 to your computer and use it in GitHub Desktop.
Hide products from users who are not logged in using tags in WooCommerce
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
function wc_exclude_by_product_tag( $query ) { | |
if ( $query->is_main_query() && is_woocommerce() && !is_user_logged_in() ) { | |
$taxquery = array( | |
array( | |
'taxonomy' => 'product_tag', | |
'field' => 'id', | |
'terms' => array( 6 ), // the ID of the product tag | |
'operator'=> 'NOT IN' // exclude | |
) | |
); | |
$query->set('tax_query', $taxquery); | |
} | |
} | |
add_action( 'pre_get_posts', 'wc_exclude_by_product_tag' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/34684881/hide-products-from-users-who-are-not-logged-in-using-tags/