01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | add_filter( 'woocommerce_product_data_store_cpt_get_products_query' , 'handle_price_range_query_var' , 10, 2 ); function handle_price_range_query_var( $query , $query_vars ) { if ( ! empty ( $query_vars [ 'price_range' ] ) ) { $price_range = explode ( '|' , esc_attr( $query_vars [ 'price_range' ]) ); if ( is_array ( $price_range ) && count ( $price_range ) == 2 ) { $query [ 'meta_query' ][ 'relation' ] = 'AND' ; $query [ 'meta_query' ][] = array ( 'key' => '_price' , 'value' => reset( $price_range ), // From price value 'compare' => '>=' , 'type' => 'NUMERIC' ); if ( end ( $price_range ) != '' ){ $query [ 'meta_query' ][] = array ( 'key' => '_price' , 'value' => end ( $price_range ), // To price value 'compare' => '<=' , 'type' => 'NUMERIC' ); } $query [ 'orderby' ] = 'meta_value_num' ; // sort by price $query [ 'order' ] = 'ASC' ; // In ascending order } } return $query ; } |
thanks to