How can I set multiple meta keys order by meta value, can any one give me some example?
-
4Welcome to WPSE - for us to help you, you need to help us. Can you describe in more detail what you are trying to do, and perhaps post some code samples.TheDeadMedic– TheDeadMedic2016-11-16 09:44:21 +00:00Commented Nov 16, 2016 at 9:44
-
I don't understand why this was closed or requested more info, this is very straightforward clear and with right tags...jave.web– jave.web2021-06-10 10:07:09 +00:00Commented Jun 10, 2021 at 10:07
Add a comment
|
1 Answer 1
meta_query is an array of meta clauses. For example:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'state',
'value' => 'Wisconsin',
),
array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
) );
You can use an associative array, with a key for each meta clause:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
) );
Then, you can use that keys in the order_by argument, with one:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => 'city_clause', // Results will be ordered by 'city' meta values.
) );
Or more clauses:
$q = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'state_clause' => array(
'key' => 'state',
'value' => 'Wisconsin',
),
'city_clause' => array(
'key' => 'city',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
) );
Example taken from this post in Make WordPres Core blog.
-
do not forget to add the type of the meta key/value. it will affect the results. by default wp will treat your meta as a string.Alexander Ivashchenko– Alexander Ivashchenko2018-12-12 11:54:03 +00:00Commented Dec 12, 2018 at 11:54
-
-
7But what if I don't want to only return results where the state is Wisconsin? I want all states to be returned and and I want rows where there may or may not be a city and still order by those 2 fields. e.g. no WHERE clause at all - just an ORDER BY.Felix Eve– Felix Eve2019-05-09 09:40:14 +00:00Commented May 9, 2019 at 9:40
-
Felix, with those requirements a workaround might be to include a final 'catch all' in your meta_query, and put it last in your orderby statement. So the first few clauses in your meta_query would get the specific results you want to order by first. Then your last clause would be everything that is not your previous clauses.Matt Keys– Matt Keys2020-10-02 19:30:13 +00:00Commented Oct 2, 2020 at 19:30
-
2
EXISTSis a nice way how get the complex named meta query but at the same time not filter by the value :) Btw: I had a real life use case - request was to order events by date DESC but time ASC using 1 single date&time field I used docs & this answer to build this: ideone.com/RUoyZs :-)jave.web– jave.web2021-06-10 11:23:17 +00:00Commented Jun 10, 2021 at 11:23 -
Wow this is very interestinguser2128576– user21285762022-03-05 07:00:37 +00:00Commented Mar 5, 2022 at 7:00
default
