Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created December 6, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukecav/f638dabb43db94e360b043f5f66bfbc6 to your computer and use it in GitHub Desktop.
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
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