Austin Verburg
Austin Verburg

Reputation: 75

Wordpress Tax Query with Relation OR not working

I am running a wordpress loop that grabs posts from two post types.

I need the loop to grab posts with certain categories set. As it's two separate post types, they both have a category taxonomy. When a user selects one of the categories on the front end, I want the loop to grab all posts from one post type with that category under it's respective taxonomy, as well as all posts from the other post type with that category under its taxonomy.

To simplify the description of what I'm trying to do:

Getting the loop to grab from both post types has not been an issue, but I'm stuck on trying to get the loop to grab only posts with specific categories. I'm trying out a 'tax_query' but the loops seems to just be ignoring it entirely, as if I didn't put the tax_query at all.

Running either 'category_name' or 'quiz_category' on their own outside of the tax_query works just fine. But within the tax_query, they're both completely ignored.

    $cat = $_GET['cat'];
    $cat = sanitize_text_field($cat);

    $args = array(
        'post_type' => array('post', 'quizzes'),
        'posts_per_page' => -1,
        'category__not_in' => array( 9, 10 ),
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category_name',
                'field'    => 'slug',
                'terms'    => $cat
            ),
            array(
                'taxonomy' => 'quiz_category',
                'field'    => 'slug',
                'terms'    => $cat
            ),
        )
    );

Upvotes: 3

Views: 3331

Answers (1)

Noah
Noah

Reputation: 570

I think the problem is that you’re passing a string for the term slug value.

Outside the tax_query, I think you can pass either a string or an array. Inside the tax_query, with multiple taxonomies, the only examples I see in the documentation the values are wrapped in arrays.

The docs don't specifically state this anywhere that I can find. However, if you could just use a string instead of an array, this snippet that I copied directly from the docs linked above would be the place it should be shown:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'quotes' ),
        ),
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array( 'post-format-quote' ),
        ),
    ),
);
$query = new WP_Query( $args );


All that being said, your code should look something like this:

  $cat = $_GET['cat'];
    $cat = sanitize_text_field($cat);

    $args = array(
        'post_type' => array('post', 'quizzes'),
        'posts_per_page' => -1,
        'category__not_in' => array( 9, 10 ),
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category_name',
                'field'    => 'slug',
                'terms'    => array($cat)
            ),
            array(
                'taxonomy' => 'quiz_category',
                'field'    => 'slug',
                'terms'    => array($cat)
            ),
        )
    );

If that doesn’t work, can you echo the $cat variable and post the result?

Upvotes: 2

Related Questions