Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 http://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' );

}

share|improve this question
add comment

2 Answers

When developing with WooCommerce just look into plugin files. I 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)
) );

It's a simple query so it will be easy to exclude what you want.

share|improve this answer
add comment

I've developed a plugin that may do what you need: http://www.wpworking.com/product/woocommerce-exclude-categories-pro/

All the best!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.