Vinaya Maheshwari
Vinaya Maheshwari

Reputation: 575

How to order by custom post type by two custom field

Here is my meta query to filter properties by property_type and bedrooms which are order by price in DESC order.

$properties = array(
        'post_type' => 'soto_property',
        'paged'=>$paged,
        'posts_per_page' => 6,
       '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',
        );
    }
    else
    {
        $properties['meta_query'][] = array(
            'key' => 'price',
            'compare' => 'EXISTS'
        );
    }



    /*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 ASC');
            }
        add_filter('posts_orderby','orderbyreplace');

        query_posts($properties);
        remove_filter('posts_orderby','orderbyreplace');

It works correctly but now I want to order properties also by bedrooms.

For example if bedroom value is selected as "2" then all properties filtered which have bedrooms value greater than 2.

I want to order properties like first all properties with bedrooms value 2 filtered in price order(DESC) then all properties with bedrooms value 3 filtered in price order(DESC) and then similarly for bedrooms value 4,5 and 6.

How to do that?

Upvotes: 0

Views: 1219

Answers (2)

Touqeer Shafi
Touqeer Shafi

Reputation: 5264

As far as i know multiple sorting with meta_values is not available in WP_Query.

However you can use posts_orderby filter and here it's example

Upvotes: 2

Junaid Ahmed
Junaid Ahmed

Reputation: 695

    function my_pre_get_posts( $query ) {

        // do not modify queries in the admin
        if( is_admin() ) {

            return $query;

        }


        // only modify queries for 'price' post type
        if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'property' ) {
            $price =get_post_meta($post_id, 'price', true);
if($price){
            $query->set('orderby', 'meta_value_num');   
            $query->set('meta_key', 'price');    
            $query->set('order', 'DESC'); 
            }
else
{
$query->set('orderby', 'meta_value_num');   
            $query->set('meta_key', 'bed');  
            $query->set('order', 'DESC'); 
}
        }


        // return
        return $query;

    }

add_action('pre_get_posts', 'my_pre_get_posts');

You can use this in your functions.php it will order your record by custom field 'price'

Upvotes: 0

Related Questions