Query not sorting on meta_value field
-
Hi,
I have got a WP site with lots of real estate properties. I would like to order them on the price of the properties in descending order. The prices are stored in _property_price. However when I fire the query it displays them in the order in which I have published them. Someone can have a look at my php code?
Thanks
[ Moderator note: code fixed. Please wrap code in the backtick character without the blockquote. ]
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $posts_per_page = $tpl_default_settings['product-post-per-page']; $wp_query = new WP_Query(); $properties = array( 'post_type' =>'dt_properties', 'posts_per_page' =>$post_per_page, 'paged' =>$paged, 'tax_query' =>array(), 'meta_query' =>array(), 'order' => 'DESC', 'order_by' => 'meta_value_num', 'meta_key' => '_property_price'); #Post Title if( isset($_REQUEST['pname']) ): # search_proprty_title - custom arg please refer property_title_filter() $properties['search_proprty_title'] = $_REQUEST['pname']; add_filter( 'posts_where', 'property_title_filter', 10, 2 ); endif; #Contract Type if( isset($_REQUEST['searchby']) && $_REQUEST['searchby'] !== "default" ): $contract_type_id = get_term_by('slug',$_REQUEST['searchby'],'contract_type',ARRAY_A); $contract_type_id = is_array( $contract_type_id ) ? $contract_type_id['term_id'] : ""; $properties['tax_query'][] = array( 'taxonomy' => 'contract_type', 'field' => 'id', 'terms' => $contract_type_id, 'operator' => 'IN',); endif; #Location if( !empty( $_REQUEST['plocation']) && $_REQUEST['plocation'] > 0 ) { $properties['tax_query'][] = array( 'taxonomy' => 'property_location', 'field' => 'id', 'terms' => $_REQUEST['plocation'], 'operator' => 'IN',); } #Property Type if( !empty( $_REQUEST['ptype']) && $_REQUEST['ptype'] > 0 ) { $properties['tax_query'][] = array( 'taxonomy' => 'property_type', 'field' => 'id', 'terms' => $_REQUEST['ptype'], 'operator' => 'IN',); } # Beds Meta if( !empty( $_REQUEST['pbeds']) && $_REQUEST['pbeds'] > 0 ) { $properties['meta_query'][] = array( 'key' => '_bedrooms', 'value' => $_REQUEST['pbeds'], 'compare' => '>=', 'type' => 'numeric',); } #Bath Meta if( !empty( $_REQUEST['pbaths']) && $_REQUEST['pbaths'] > 0 ) { $properties['meta_query'][] = array( 'key' => '_bathrooms', 'value' => $_REQUEST['pbaths'], 'compare' => '>=', 'type' => 'numeric',); } #Floors Meta if( !empty( $_REQUEST['pfloors']) && $_REQUEST['pfloors'] > 0 ) { $properties['meta_query'][] = array( 'key' => '_floors', 'value' => $_REQUEST['pfloors'], 'compare' => '>=', 'type' => 'numeric',); } #Parking Meta if( !empty( $_REQUEST['pgarages']) && $_REQUEST['pgarages'] > 0 ) { $properties['meta_query'][] = array( 'key' => '_parking', 'value' => $_REQUEST['pgarages'], 'compare' => '>=', 'type' => 'numeric',); } #Area $minarea = isset($_REQUEST['minarea']) ? $_REQUEST['minarea'] : 0; if( $minarea > 0 ){ $properties['meta_query'][] = array( 'key' => '_area', 'value' => $minarea, 'type' => 'numeric', 'compare' => '>='); } #Perceel $minperceel = isset($_REQUEST['minperceel']) ? $_REQUEST['minperceel'] : 0; if( $minperceel > 0 ){ $properties['meta_query'][] = array( 'key' => '_perceel', 'value' => $minperceel, 'type' => 'numeric', 'compare' => '>='); } #Price $minprice = isset($_REQUEST['minprice']) ? $_REQUEST['minprice'] : 0; $maxprice = isset($_REQUEST['maxprice']) ? $_REQUEST['maxprice'] : 0; if( $minprice > 0 && $maxprice > 0 ){ $properties['meta_query'][] = array( 'key' => '_property_price', 'value' => array( $minprice, $maxprice ), 'type' => 'numeric', 'compare' => 'BETWEEN'); }elseif( $minprice > 0 ){ $properties['meta_query'][] = array( 'key' => '_property_price', 'value' => $minprice, 'type' => 'numeric', 'compare' => '>='); }elseif( $maxprice > 0 ){ $properties['meta_query'][] = array( 'key' => '_property_price', 'value' => $maxprice, 'type' => 'numeric', 'compare' => '<='); } $wp_query->query( $properties );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Query not sorting on meta_value field’ is closed to new replies.