Pergunta

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);
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top