wp wc php out of stock product to bottom

Solutions on MaxInterview for wp wc php out of stock product to bottom by the best coders in the world

showing results for - "wp wc php out of stock product to bottom"
Arthur
10 May 2020
1add_filter('posts_clauses', 'order_by_stock_status');
2function order_by_stock_status($posts_clauses) {
3    global $wpdb;
4    // only change query on WooCommerce loops
5    if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
6        $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
7        $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
8        $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
9    }
10    return $posts_clauses;
11}
12