Skip to content

Instantly share code, notes, and snippets.

@KoolPal
Forked from felipe-pita/functions.php
Created February 5, 2020 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KoolPal/950231cd9f9d2b145690e61715c5d27c to your computer and use it in GitHub Desktop.
Save KoolPal/950231cd9f9d2b145690e61715c5d27c to your computer and use it in GitHub Desktop.
<?php
/**
* Ordena produtos fora de estoque no final do catálogo
* source: https://stackoverflow.com/a/44597448
*/
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment