I want to exclude sale products from output related products.

In $args i have field post__not_in where i want to output array of values from wc_get_product_ids_on_sale(); function.

Code before $args

$related = $product->get_related( $posts_per_page );    
$product_ids_on_sale = wc_get_product_ids_on_sale();

After i wrote args

$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'         => $product_ids_on_sale,
        ) );

But in frontend i see products not in sale and in sale

I try to echo $product_ids_on_sale it returns Array As i understand, i need to return each value from array $product_ids_on_sale separated with , Than i try to write foreach like

$product_ids_on_sale = wc_get_product_ids_on_sale();
         foreach($product_ids_on_sale as $result) {
            echo $res,',';

But echo is not i want. I need to return it Also i try to write to 'post__not_in'

array_merge( array( 0 ), wc_get_product_ids_on_sale() )

Didn't help...

Please help me to exclude sale products!

1 Answer 1


If you have both post__in and post__not_in as input arguments for WP_Query, then only post__in is activated. See the source here.

If you need to exclude IDs from the post__in, you should do it before passing on the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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