0

Help me figure out how to structure this meta_query within my post query!

There are two taxonomies attached to my post type ('audition_type', and 'union_requirement'). On a page of my site the user is able to use checkboxes to filter a query of the post type ('audition'). The below meta_query is working fine to get all posts that are in the terms that the user selects but it's not quite right. I need it to return all the posts that match ANY of the terms selected in the 'audition_type' taxonomy and then narrow it down further by only getting posts that have the selected 'union_requirement' terms selected . So, almost like a query within a query. That's where I can't quite figure out the logic.

'meta_query' => array(
    'relation' => 'OR',
    array(
    'key' => 'audition_type',
    'value' => $typeIDs,
    ),
    array(
    'key' => 'union_requirement',
    'value' => $unionIDs
    )
),

$typeIDs and $unionIDs variables are arrays containing the term IDs if that wasn't obvious.

0

If I follow your description right you want posts which have any $typeIDs, but all of $unionIDs ?

Aside from wrong meta_query choice, you miss operator argument which specifies which kind of match you want.

I think your query should be something like this:

'tax_query' => array(
    'relation' => 'AND', // you want both conditions to match
    array(
    'taxonomy' => 'audition_type',
    'terms' => $typeIDs,
    'operator' => 'IN', // this is default, just showing you difference
    ),
    array(
    'taxonomy' => 'union_requirement',
    'terms' => $unionIDs,
    'operator' => 'AND', // not default, we want matches to all terms
    )
),

See Taxonomy Parameters in Codex for further documentation.

0

Sorry for the delay on this everyone, I was out of town! Here is what I figured out and am using today. I had to check for the cases where one taxonomy was selected and one was not, which actually only requires a simple tax query, but that was giving me null values and breaking the query, so I had to use some conditionals instead to build it.

$typeIDs and $unionsIDs are arrays of taxonomies that are selected.

Thanks for your suggestions!

//Set the proper tax_query based on what we're looking for
if ($typeIDs && $unionIDs) {
    $args['tax_query'] = array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'audition_type',
            'field' => 'term_id',
            'terms' => $typeIDs,
        ),
        array(
            'taxonomy' => 'union_requirement',
            'field' => 'term_id',
            'terms' => $unionIDs,
            'operator' => 'AND'
        )
    );
} elseif ($typeIDs) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'audition_type',
            'field' => 'term_id',
            'terms' => $typeIDs,
        )
    );
} else if ($unionIDs) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'union_requirement',
            'field' => 'term_id',
            'terms' => $unionIDs,
        )
    );
} else {
    $args['tax_query'] = false;
}
-2

Try this code, meta query will not give you the results you want. Please go through the meta query for more details. Meta query Or go here for reference.

<?php 
$myquery = array(
                'numberposts'=>-1,
                'tax_query' => array(
                'relation' => 'OR',
                            array(
                                'taxonomy' => 'audition_type',
                                'terms' => $typeIDs
                            ),
                            array(
                                'taxonomy' => 'union_requirement',
                                'terms' => $unionIDs
                            ),
                ),
            );
query_posts($myquery);

?>

  • Downvoted because of 1.) The use of query_posts. NEVER EVER use query_posts. 2.) Please explain what your code does and how it works, this is just a useless code dump – Pieter Goosen May 6 '16 at 5:22

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.