add_action( 'pre_get_posts', 'njengah_hide_out_of_stock_products' ); function njengah_hide_out_of_stock_products( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) { $tax_query = (array) $query->get('tax_query'); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'term_taxonomy_id', 'terms' => array( $outofstock_term->term_taxonomy_id ), 'operator' => 'NOT IN' ); $query->set( 'tax_query', $tax_query ); } remove_action( 'pre_get_posts', 'njengah_hide_out_of_stock_products' ); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter