Created
January 28, 2020 06:40
-
-
Save kartikparmar/c95722ce82b79d44e2c084d0577b3426 to your computer and use it in GitHub Desktop.
Hide categories from shop page only for guest users.
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
<?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