Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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!

    #1228071

    Hey 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,
    Mike

    #1228107

    Hi 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.

    #1228562

    Hi,
    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,
    Mike

    #1228615

    Hi 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!

    #1229131

    Hi,
    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,
    Mike

    #1229200

    Hi 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?

    #1229423

    Hi,
    Glad to hear this helps, shall we close this then?
    I will take a look at your other thread.

    Best regards,
    Mike

    #1229460

    Hi Mike,
    Yes, this thread can be closed.
    Thanks a lot for your help! :-)

    #1229636

    Hi,
    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

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Hide one specific category from product_meta on product pages’ is closed to new replies.