I am working on real state property website. I have CPT “soto_property”. I also have two custom fields for this CPT named “price” and “dorm_room”. I have a search form, from where visitor can filter properties with different price and dorm_room.
So what I want is that if visitor search for 2 dorm_room, IT SHOULD SHOW ALL THE 2 dorm_room 1ST IN PRICE ORDER FROM HIGH TO LOW AND THEN 3 dorm_room FROM HIGH TO LOW price and THEN 4 DORM ROOM WITH HIGH TO LOW PRICE .
I have followed many links shown below but no luck –
http://www.billerickson.net/code/order-results-multiple-meta_keys/
https://gist.github.com/lolitaloco/5441726
here is my code-
$properties = array(
'post_type' => 'soto_property',
'paged'=>$paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'price'
);
/*check Price range */
if (!empty($_GET['price_range'])) {
$lowestPrice=get_post_meta($_GET['price_range'],"lowest_price",true);
$maximumPrice=get_post_meta($_GET['price_range'],"maximum_price",true);
$properties['meta_query'][] = array(
'key' => 'price',
'value' => array($lowestPrice, $maximumPrice),
'type' => 'numeric',
'compare' => 'BETWEEN',
);
}
/*check bedrooms range */
if (!empty($_GET['bedrooms'])) {
$beds_value=get_post_meta($_GET['bedrooms'],"beds_value",true);
if($beds_value)
{
$properties['meta_query'][] = array(
'key' => 'dorm_room',
'value' => $beds_value,
'type' => 'numeric',
'compare' => '>='
);
}
}
function orderbyreplace($orderby ) {
global $wpdb;
return ('mt1.meta_value DESC, mt2.meta_value DESC');
}
add_filter('posts_orderby','orderbyreplace');
query_posts($properties);
remove_filter('posts_orderby','orderbyreplace');
This code sorts property posts by price from High to low but not with dorm_room.