5

Based on example here https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

I'd like to modify the query

$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') );

to be able to get all posts where state is 'Wisconsin' OR timezone is 'central' ORDER BY population DESC

  • Please do not ask for the same thing in two different threads :) – fischi Feb 24 '16 at 13:14
  • Sure - sorry about that – dkatwp Feb 25 '16 at 17:33
9

You can create groups of meta_queries using specific compare operation on them, and since you want to order based in a single custom field, you can keep the order declaration dedicated to the single meta field. So:

$q = new WP_Query( 
    array(
        'meta_key' => 'population', //setting the meta_key which will be used to order
        'orderby' => 'meta_value', //if the meta_key (population) is numeric use meta_value_num instead
        'order' => 'DESC', //setting order direction
        'meta_query' => array(
            'relation' => 'AND', //setting relation between queries group
            array(
                'relation' => 'OR', //setting relation between this inside query
                array(
                    'key' => 'state',
                    'value' => 'Wisconsin',
                ),
                array(
                    'key' => 'timezone',
                    'value' => 'central',
                )
            ),
            array(
                'key' => 'city',
                'compare' => 'EXISTS',
            )
        )
    )           
);
|improve this answer|||||
1

This would be done like this:

$q = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'relation' => 'OR',
            'state_clause' => array(
                'key' => 'state',
                'value' => array( 'Wisconsin' ), //allowed values
                'compare' => 'IN' // state must be in array above
            ),
            'state_clause' => array(
                'key' => 'timezone',
                'value' => 'central',
                'compare' => '='
            ),
        ),
        'city_clause' => array(
            'key' => 'city',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array(
        'city_clause' => 'DESC',
    ),
) );
|improve this answer|||||
  • Thanks @fischi - but I have two problems with that solution: 1) the value of 'state_clause' in meta_query array gets overwritten - so only last declaration stays in ('timezone') if u actually dump the query. 2) the 'relation' => 'OR' ruins the logic of a query - now result is all posts as population exists in all cases (and we just want the ones where state is 'Wisconsin' or timezone is 'central' – dkatwp Feb 24 '16 at 13:32
  • see answer for update. – fischi Feb 24 '16 at 14:11
  • Thank you! repeating 'state_clause' in the array for state and timezone keys would not work - so I needed to do it as @Bruno Cantuaria sugested above. However, Bruno's solution did not work for me without declaring city_clause and calling it in 'orderby'. So thanks again – dkatwp Feb 24 '16 at 15:06
  • No Problem. Glad to help! – fischi Feb 24 '16 at 15:23

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.