Showing products in-stock only
WooCommerce - Showing products in-stock only

Solution 1 (easy)

This solution is purely configuration based, highly recommended to use. Use solution 2 only if this is not working for some reason. Step 1: Open up the Frontend search settings -> Custom fields panel
Step 2: Make a hidden filter, on the _stock_status custom field, with the value of “instock”:
After saving the options, the filter should be working.

Solution 2 (custom code)

I suggest using this solution if the first one is not working.
1
add_filter( 'asp_pagepost_results', 'asp_stock_status_filter', 10, 1 );
2
function asp_stock_status_filter( $pageposts ) {
3
foreach ($pageposts as $k=>$v) {
4
5
// Get the stock status
6
$stock_status = get_post_meta( $v->id, '_stock_status', true);
7
if ( empty($stock_status) || $stock_status == "instock" )
8
continue;
9
10
unset($pageposts[$k]);
11
}
12
return $pageposts;
13
}
Copied!
Last modified 1yr ago