• I’m trying to exclude products from two specific categories from showing up in the related products on the content single products page. The closest idea I have stumbled upon is this code from https://docs.woothemes.com/document/exclude-a-category-from-the-shop-page/. Any idea how to modify it for related products and NOT for the shop page?

    add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
    function custom_pre_get_posts_query( $q ) {
    
    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    
    if ( ! is_admin() && is_shop() ) {
    
        $q->set( 'tax_query', array(array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array( 'knives' ), // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
        )));
    
    }
    
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );}

    https://www.ads-software.com/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter newmedia85

    (@newmedia85)

    I also found a file named related.phpin woocommerce\template\single-product\ :

    $args = apply_filters('woocommerce_related_products_args', array(
    'post_type'     => 'product',
    'ignore_sticky_posts'   => 1,
    'no_found_rows'     => 1,
    'posts_per_page'    => $posts_per_page,
    'orderby'       => $orderby,
    'post__in'      => $related,
    'post__not_in'      => array($product->id)
    ) );

    Not really strong in wordpress. This is all new to me so any ideas or pointer will be very appreciated.

    Thread Starter newmedia85

    (@newmedia85)

    No help from anyone?

    Did you figure this out?
    I’m having a similar issue…

    In the related.php file you mentioned, at the end of the apply filters code, paste this :
    ‘post__not_in’ => array($product->id),
    ‘tax_query’ => array(array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array( ‘sold’ ), // Don’t display products in the sold category in related products
    ‘operator’ => ‘NOT IN’))

    I left the ‘post_not_in’ line in there so you could see where the ‘tax_query’ portion needs to be added.
    You will need to change the ‘sold’ text to whatever the slug is for the category you want to exclude. Enclose the slugs in single quotes (‘), separate multiple categories by commas eg. ‘mycategory1′,’mycategory2′,’mycategory3’

    One side effect I have found with this code is that it will break the loop when it hits an excluded category, so if you normally display 4 related products, it may only display 1, or 2, or maybe even none…or it may display all 4. Refreshing the page will change the related products and generate a new loop, thus the number of relateds displayed. Not a particularly nice side effect, but better than displaying related products that aren’t in stock ??

    Hi, I would like to be able to use a filter for this in my custom-functions.php as first proposed by newmedia85, instead of modifying a template as indicated above. Is it feasible?

    Would anyone know how to modify the code of newmedia85?

    Thanks, any help would be appreciated ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘exclude specific category products from related products’ is closed to new replies.