0

Note : This is not a duplicate question to If value present, order posts by two consecutive custom fields. After filtering a custom filed, I need to order by another custom filed. (The above link explains filtering by two custom fields. But I need to filter by a custom filed and then order by another custom filed.

This is a my current wp query array.

$args = array(
 'post_type' => array('post', 'video'),
 'meta_key' => 'pp_like_count',
 'orderby'  => 'meta_value_num',                        
);

I have order by custom filed called pp_like_count. This works fine.

But I need to display only if 'pp_lang' custom filed equals to 'english'. How to modify this to add it?

(Order by pp_like_count also should be there.)

  • @toscho This is not a duplicate question to wordpress.stackexchange.com/questions/222148/…. After filtering a custom filed, I need to order by another custom filed. (The above link explains filtering by two custom fields. But I need to filter by a custom filed and then order by another custom filed. – Sasa1234 Apr 10 '16 at 16:43
1

You can try this by using meta query to filter results and then adding orderby to order results. Below arguments should work

$args = array(
 'post_type' => array('post', 'video'),
 'meta_key' => 'pp_like_count',
 'orderby'  => 'meta_value_num',
 "meta_query" => array(            
        array(
            "key" => "pp_lang",
            "value" => "english",
            "type" => "CHAR",
            "compare" => "="
        ),
    ),                 
);
  • Thanks, I thought it would not works because I thought I can't use both meta_key and meta_query in same array. But it worked. Thanks :-) – Sasa1234 Apr 13 '16 at 3:10
  • 1
    never assume anything before trying :) – Deepak jha Apr 13 '16 at 12:18
0

You can use meta query see below link it will help you.

How to order by meta field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.