sort out of stock products at the end in search results
-
Hello,
I am trying to sort out of stock products at the end of product list in search results page, but I can’t. I have add this code:
`function _nok_order_by_stock_status( $posts_clauses, $query ) {
// only change query on WooCommerce loops
if ( $query->is_main_query() && ( is_search() || is_product_category() || is_product_tag() || is_product_taxonomy() || is_shop() ) ) {
global $wpdb;$posts_clauses[‘join’] .=
” LEFT JOIN (
SELECT post_id, meta_id, meta_value FROM $wpdb->postmeta
WHERE meta_key = ‘_stock_status’ AND meta_value <> ”
) istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) “;$posts_clauses[‘orderby’] =
” CASE istockstatus.meta_value WHEN
‘outofstock’ THEN 1
ELSE 0
END ASC, ” . $posts_clauses[‘orderby’];
}return $posts_clauses;
}
add_filter( ‘posts_clauses’, ‘_nok_order_by_stock_status’, 2000, 2 );It works perfectly on product categories, but in search results nothing goes right.
What can I do?
Please help,
thanks in advance.The page I need help with: [log in to see the link]
- The topic ‘sort out of stock products at the end in search results’ is closed to new replies.