Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Musilda/16a72c08b0d1854a90984beb2d353a69 to your computer and use it in GitHub Desktop.
Save Musilda/16a72c08b0d1854a90984beb2d353a69 to your computer and use it in GitHub Desktop.
<?php
add_action( 'pre_get_posts', 'musilda_hide_out_of_stock_products' );
function musilda_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'musilda_hide_out_of_stock_products' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment