Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created January 28, 2020 06:40
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 kartikparmar/c95722ce82b79d44e2c084d0577b3426 to your computer and use it in GitHub Desktop.
Save kartikparmar/c95722ce82b79d44e2c084d0577b3426 to your computer and use it in GitHub Desktop.
Hide categories from shop page only for guest users.
<?php
/**
* Show products only of selected category.
*/
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
$hide_category = array();
if ( ! is_user_logged_in() ) { // Hide below mentioned categories for guest user
$hide_category = array( 126 ); // Ids of the category you don't want to display on the shop page
}
// if 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->term_id, $hide_category ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment