I don't want to display "out of stock" products on my home page. I have tried some WooCommerce hooks and filter to alter product query but its not working. I have also checked "hide out of stock" into woocommerce setting area.
but product are still appearing. Can I get the clue, wh its happening.
I tried this filter hook to alter main product query:
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() ) {
$q->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
I want to hide out of stock products but nothing is working. Any help is appreciated.