0

i have 5 sets of meta boxes in some custom postype. So.. i know how to compare two meta box values...

Example:

'meta_query'        => array(
    'relation'  => 'AND',
    array(
        'key'     => THEME_PREF.'source_lang_1',
        'value'   => $sourceLang,
        'compare' => '='
    ),
    array(
        'key'     => THEME_PREF.'target_lang_1',
        'value'   => $targetLang,
        'compare' => '='
    ),
),

But...
what if i need to check 4 fields - the verification isnt true..
meaning it doesnt returnes only the right results
only "posts" that answer both conditions.

$transargs = array(
    'post_type'         =>  'translators',
    'orderby'           =>  'date',
    'order'             =>  'DESC',
    'posts_per_page'    =>  -1,
    'meta_query'        => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND',
            array(
                'key'       =>  THEME_PREF.'source_lang_1',
                'value'     =>  $sourceLang,
                'compare'   =>  '='
            ),
            array(
                'key'       =>  THEME_PREF.'target_lang_1',
                'value'     =>  $targetLang,
                'compare'   =>  '='
            )
        ),
        array(
            'relation' => 'AND',
            array(
                'key'       =>  THEME_PREF.'source_lang_2',
                'value'     =>  $sourceLang,
                'compare'   =>  '='
            ),
            array(
                'key'       =>  THEME_PREF.'target_lang_2',
                'value'     =>  $targetLang,
                'compare'   =>  '='
            )
        ),
    )   
);
$trns_query = new WP_Query($transargs);

To elaborate
i get a lot of results where the first condition of each meta query is met but not the second one and thus the relation and

Example:
$sourceLang = 1;
$targetLang = 2;

i might also get
(which is not good... the AND doesnt work?)...
result that equals $sourcelang(1) / targetLang(5)

5
  • Your code looks proper for the 4 post meta query. Could you please explain what exactly are you looking for?
    – Domain
    Oct 31, 2014 at 9:49
  • do you mean this would work as i? this is just the example code of what am trying to achieve... Would run to test it now.
    – Sagive
    Oct 31, 2014 at 14:18
  • it doesnt return the right results... the compare doesnt work or i dont understand how to verify both condition...
    – Sagive
    Dec 4, 2014 at 13:17
  • 2
    Nested relations among subset of meta queries are possible on WP 4.1 that will be released next week. See trac ticket. Consider to use beta version.
    – gmazzap
    Dec 4, 2014 at 13:52
  • thats cool #G.M so, this is currently not supported in wp. i see, i cant use the beta, my WP is in hebrew. Thanks a lot for this info. Submit as answer to claim the reward ;)
    – Sagive
    Dec 4, 2014 at 14:51

1 Answer 1

1
+50

Nested relations among subset of meta queries are possible on WP 4.1 that is planned to be releaded next coming December 10, 2014. See trac ticket.

So wait a few days or use the beta version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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