WC_Widget_Products::query_products
Run the widget query, ordering through the product lookup table where that is equivalent to, and cheaper than, ordering through post meta.
Only the sales ordering is redirected. total_sales post meta is written for every product, so the meta join it replaces filters nothing out, while wc_product_meta_lookup holds one narrow row per product instead of one row per product in a table that grows with every extension's meta. Price ordering deliberately stays on post meta; see the price case in get_products().
The swap happens after woocommerce_products_widget_query_args run, and only when nothing on that filter changed the ordering arguments, so the filter payload keeps its documented shape and a consumer that overrides the ordering still wins.
Метод класса: WC_Widget_Products{}
Хуков нет.
Возвращает
WP_Query.
Использование
// private - только в коде основоного (родительского) класса $result = $this->query_products( $query_args );
- $query_args(массив) (обязательный)
- Query arguments, as returned by the widget query args filter.
Код WC_Widget_Products::query_products() WC Widget Products::query products WC 11.3.0
private function query_products( $query_args ) {
// WP_Query skips posts_clauses when a filter asks for suppressed filters, which would leave the
// query with no ordering at all, so stay on the post meta ordering in that case.
$orders_by_total_sales = isset( $query_args['meta_key'], $query_args['orderby'] )
&& empty( $query_args['suppress_filters'] )
&& 'total_sales' === $query_args['meta_key']
&& 'meta_value_num' === $query_args['orderby'];
if ( ! $orders_by_total_sales ) {
return new WP_Query( $query_args );
}
$order = isset( $query_args['order'] ) && 'asc' === strtolower( $query_args['order'] ) ? 'ASC' : 'DESC';
unset( $query_args['meta_key'] );
$query_args['orderby'] = 'none';
$products = new WP_Query();
$order_by_total_sales = static function ( $clauses, $query ) use ( $products, $order ) {
global $wpdb;
if ( $query !== $products ) {
return $clauses;
}
if ( ! strstr( $clauses['join'], 'wc_product_meta_lookup' ) ) {
$clauses['join'] .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON {$wpdb->posts}.ID = wc_product_meta_lookup.product_id ";
}
// The IS NULL term sorts ascending in both directions, so products that have no lookup row
// (a partly regenerated table, say) always land last instead of leading an ascending list.
$clauses['orderby'] = " wc_product_meta_lookup.total_sales IS NULL, wc_product_meta_lookup.total_sales {$order}, wc_product_meta_lookup.product_id {$order} ";
return $clauses;
};
add_filter( 'posts_clauses', $order_by_total_sales, 10, 2 );
$products->query( $query_args );
remove_filter( 'posts_clauses', $order_by_total_sales, 10 );
return $products;
}