1

I'm trying to use some conditional logic to compare custom fields in my query, but a bit stuck. How would I get all posts with ReleasedProject AND PermanentArtist set to 'true'?

Here's what I have so far, I'm assuming it's something to do with 'compare':

Thanks

osu

EDIT: To clarify what I want to do - I'm trying to exclude pages that have two custom field values (ReleasedProject and PermanentArtist in this case) set to 'false'. These are set through checkboxes generated using the plugin Custom Field Template.

I've updated the code to illustrate that I need to filter out all pages with these two custom fields set to false:

global $post;
$artist_args = array(
    'post_type' => 'page',
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'ReleasedProject',
            'value' => 'false'
        ),
        array(
            'key' => 'PermanentArtist',
            'value' => 'false'
        )
    )
);
$my_query = new WP_Query($artist_args);
8
  • I cannot see any problem with that query and should work as you have asked.
    – Scott
    Commented May 10, 2011 at 13:27
  • This is a bit confusing, do you want to compare the 2 meta box values with each other or do you just want to query and display them if they contain a value ( not compare them).
    – Wyck
    Commented May 10, 2011 at 13:28
  • Have you done a print_r($my_query) to see if anything is returned? Try doing this without meta_query and see if any pages are returned...
    – Scott
    Commented May 10, 2011 at 13:30
  • 1
    You say you want to exclude posts but you show code with 'post_type' => 'page',. You are aware you have this set to page and not post
    – Scott
    Commented May 10, 2011 at 14:21
  • 1
    Plus all you have done is change the logic with your edit... before you said you want to only show posts that have ReleasedProject AND PermanentArtist set to true. Now you want to exclude posts that have ReleasedProject AND PermanentArtist set to false. They are completly different logic... So which one do you want?
    – Scott
    Commented May 10, 2011 at 14:23

1 Answer 1

1

If you want all posts that have ReleasedProject AND PermanentArtist both set to true, you need to change the value keys for both of those to 'true'. If you want to exclude all posts that have both of those set to 'false', you need to add 'compare' => '!=' to both meta query arrays.

EDIT

The logic behind meta queries is such that all conditions must be met (it's an AND relationship). So if you say

'meta_query' => array(
    array(
        'key' => 'ReleasedProject',
        'value' => 'false'
    ),
    array(
        'key' => 'PermanentArtist',
        'value' => 'false'
    )
)

That means "find all posts that have both Released Project identical to 'false' AND Permanent Artist identical to 'false'." If you were to add the 'compare' => '!=' statement like I mentioned earlier, it would be saying "find all posts that have both Released Project NOT identical to 'false' AND Permanent Artist NOT identical to 'false'." If you have a post that has Released Project set to 'false' and Permanent artist set to 'true', it will not show up in either of those queries.

Furthermore, if a meta value is missing, it will not show up in the results either. So if you have a post with Released project set to true, and permanent artist not set at all, this meta query will not find that post:

'meta_query' => array(
    array(
        'key' => 'ReleasedProject',
        'value' => 'false',
        'compare' => '!='
    ),
    array(
        'key' => 'PermanentArtist',
        'value' => 'false'
        'compare' => '!='
    )
)

Basically, meta queries don't do 'either/or' checks, they do 'both/and' checks when you have multiple meta queries.

3
  • thanks for this, I tried this, but unfortunately it doesn't seem to work. Here are some examples of what happens: 1) pastie.org/1886125 (this shows nothing) 2) pastie.org/1886132 (this shows pages with custom field ´ReleasedProject´ 3) pastie.org/1886135 (this shows nothing). So, it seems I'm having issues trying to get ´meta_query´ to work with two custom fields. Any ideas?
    – Osu
    Commented May 10, 2011 at 19:06
  • 1
    Are you absolutely positive that you have posts that have both meta values set to 'true'? I'll update my answer with an explanation of the logic. Commented May 10, 2011 at 21:24
  • that's a good explanation. After a nights sleep and reading your answer, I realised that pages being filtered by meta_query would show nothing as each page has either ReleasedProject OR PermanentArtist set to false, not both within one page. Thanks for your help!
    – Osu
    Commented May 11, 2011 at 5:41

Your Answer

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

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