Reputation: 579
I have a product category 'Free Offers'. I want to restrict users from directly accessing this category and its products. I mean if users try to access it via url http://www.mysite.com/product-category/free-offers or http://www.mysite.com/product-category/free-offers/free-prodcut they will get a 404 error. Is it possible?
Upvotes: 0
Views: 983
Reputation: 579
I have done something and it is working fine, maybe it is useful for someone else. Here it is
function disable_free_offers_access() {
global $wp_query, $post;
$tax = 'product_cat';
$term = 'coffee-tea';
$post_type = 'product';
$term_obj = get_term_by('slug', $term, $tax);
$termchildren = get_term_children($term_obj->term_id, $tax);
if (is_tax()) {
if (is_tax($tax, $term) || term_is_ancestor_of($term_obj->term_id, $wp_query->get_queried_object()->term_id, $tax)) {
load_template(TEMPLATEPATH . '/404.php');
exit;
}
} else if (is_singular($post_type)) {
$terms = wp_get_post_terms($post->ID, $tax);
if (!empty($terms))
foreach ($terms as $t) {
if ($t->term_id == $term_obj->term_id || in_array($t->term_id, $termchildren)) {
load_template(TEMPLATEPATH . '/404.php');
exit;
}
}
}
}
add_action('template_redirect', 'disable_free_offers_access');
Upvotes: 2