Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two keys, key1 and key 2, which sometimes don't exist.

I would like that :

  • the posts which have a key 1 are classified according to the key 1,
  • the posts which have a key 1 and a key 2 are classified according to key 1,
  • the posts which do not have a key 1 but a key 2 are classified according to key 2,
  • and after posts that do not have a key.

Here is one of the tests, but which did not give the expected result:

$query = new WP_Query([
    'meta_query' => [
        'relation' => 'OR',
        ['key' => 'key1', 'compare' => 'EXISTS'],
        ['key' => 'key1', 'compare' => 'NOT EXISTS'],
        ['key' => 'key2', 'compare' => 'EXISTS'],
        ['key' => 'key2', 'compare' => 'NOT EXISTS'],
    ],
    'order' => 'DESC',
    'orderby' => 'meta_value_num',
]);

Do you know how to do it ?

question from:https://stackoverflow.com/questions/65540735/query-with-multiple-key-and-conditions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
108 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...