-
AuthorPosts
-
July 1, 2020 at 11:26 pm #1227350
Hi,
I would need to hide one specific category in product_meta / posted_in of all my products.
Which code do I need to add to achieve this?
If needed, you can find a link and credentials to my website in private content.
Thanks a lot!July 5, 2020 at 7:39 pm #1228071Hey fcp,
Sorry for the late reply and thanks for the link, which category are you trying to exclude?
Try adding this code to the end of your functions.php file in Appearance > Editor:function custom_pre_get_posts_query( $q ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'uncategorized' ), 'operator' => 'NOT IN' ); $q->set( 'tax_query', $tax_query ); } add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
and change the category “uncategorized” in the code to the category are you trying to exclude.
Then clear your browser cache and check.Best regards,
MikeJuly 5, 2020 at 11:13 pm #1228107Hi Mike,
Thanks for your reply.
I created a new category “Petits prix” (slug: petits-prix) and I added the product in private content to this category.
So I added your code like this:
function custom_pre_get_posts_query( $q ) {$tax_query = (array) $q->get( ‘tax_query’ );
$tax_query[] = array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘slug’,
‘terms’ => array( ‘petits-prix’ ),
‘operator’ => ‘NOT IN’
);$q->set( ‘tax_query’, $tax_query );
}
add_action( ‘woocommerce_product_query’, ‘custom_pre_get_posts_query’ );
…but it doesn’t work. The category tag is still visible on product page.July 7, 2020 at 12:42 pm #1228562Hi,
Sorry, I think I misunderstood, so you want to hide a specific category from the product meta, so I disabled the function above and added this one:add_filter('get_the_terms', 'hide_categories_terms', 10, 3); function hide_categories_terms($terms, $post_id, $taxonomy){ $exclude = array(361); if (!is_admin()) { foreach($terms as $key => $term){ if(in_array($term->term_id, $exclude)) unset($terms[$key]); } } return $terms; }
Now it is hiding the “Petits Prix” from the category meta, please check.
Best regards,
MikeJuly 7, 2020 at 3:19 pm #1228615Hi Mike,
Your function works perfectly! Thanks a lot!
The only last thing I need to do is hiding this category in my categories widget (second column in my footer).
Do you know which code I need to add to achieve this?
Thanks Mike!July 9, 2020 at 12:21 pm #1229131Hi,
Glad to hear, please try this code in the General Styling > Quick CSS field or in the WordPress > Customize > Additional CSS field:#woocommerce_product_categories-6 .product-categories .cat-item.cat-item-361 { display: none !important; }
After applying the css, please clear your browser cache and check.
Best regards,
MikeJuly 9, 2020 at 3:41 pm #1229200Hi Mike,
Perfect!!
Thanks a lot Mike! :-)I’m sorry to ask here, but you helped me for some issue in this thread:
https://kriesi.at/support/topic/product-grid-limit-nimber-of-products/
First, I thought everything was ok, but I realized that the function caused an issue. So I opened a new thread about this:…but I had received no solution.
For information, I made some changes on my website, and I now need to limit number of pages or products on page-id-5480 only.
Could you please help me?
July 10, 2020 at 2:04 pm #1229423Hi,
Glad to hear this helps, shall we close this then?
I will take a look at your other thread.Best regards,
MikeJuly 10, 2020 at 5:30 pm #1229460Hi Mike,
Yes, this thread can be closed.
Thanks a lot for your help! :-)July 11, 2020 at 4:17 pm #1229636Hi,
Glad we were able to help 🙂, we will close this now. Thank you for using Enfold.For your information, you can take a look at Enfold documentation here
For any other questions or issues, feel free to start new threads in the Enfold forum and we will gladly try to help you :)Best regards,
Mike -
AuthorPosts
- The topic ‘Hide one specific category from product_meta on product pages’ is closed to new replies.