Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection
QueryBuilder::get_custom_orderby_query
Return query params to support custom sort values
Method of the class: QueryBuilder{}
No Hooks.
Returns
Array.
Usage
// private - for code of main (parent) class only $result = $this->get_custom_orderby_query( $orderby );
- $orderby(string) (required)
- Sort order option.
QueryBuilder::get_custom_orderby_query() QueryBuilder::get custom orderby query code WC 10.3.6
private function get_custom_orderby_query( $orderby ) {
if ( ! in_array( $orderby, $this->custom_order_opts, true ) || 'post__in' === $orderby ) {
return array( 'orderby' => $orderby );
}
if ( 'price' === $orderby ) {
add_filter( 'posts_clauses', array( $this, 'add_price_sorting_posts_clauses' ), 10, 2 );
return array(
'isProductCollection' => true,
'orderby' => $orderby,
);
}
// The popularity orderby value here is for backwards compatibility as we have since removed the filter option.
if ( 'sales' === $orderby || 'popularity' === $orderby ) {
add_filter( 'posts_clauses', array( $this, 'add_sales_sorting_posts_clauses' ), 10, 2 );
return array(
'isProductCollection' => true,
'orderby' => $orderby,
);
}
if ( 'menu_order' === $orderby ) {
add_filter( 'posts_clauses', array( $this, 'add_menu_order_with_title_fallback_posts_clauses' ), 10, 2 );
return array(
'isProductCollection' => true,
'orderby' => $orderby,
);
}
if ( 'random' === $orderby ) {
return array(
'orderby' => 'rand',
);
}
$meta_keys = array(
'rating' => '_wc_average_rating',
);
return array(
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_key' => $meta_keys[ $orderby ],
'orderby' => 'meta_value_num',
);
}