Home › Forums › WoodMart support forum › Hide out of stock products or move them at the end › Reply To: Hide out of stock products or move them at the end
March 9, 2024 at 1:08 pm
#547655
Aizaz Imtiaz Awan
Keymaster
Hello,
You want to move the “out of stock” products at the end of the archive page? If yes, try to add the following code in the function.php of the child theme.
add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
global $wpdb;
// only change query on WooCommerce loops
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
$posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
$posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}
Best Regards.