2

I have a Custom Post Type Called movie and I have two taxonomies genre and quality associated with that. Now I need to get/filter all terms IF

genre = 'action'
AND
quality = 'HD'

Using this code I am able to get all terms which has genre term of action

$terms = get_terms( array(
                    'genre' => 'action',
                    'hide_empty' => false,
                    ) );
foreach ($terms as $term){
    echo $term->name;
}

but as I said I need to get all action terms which are also HD! Can you please let me know how to achieve this?

Thanks

1 Answer 1

1

It doesn't make any sense querying with get_terms when:

genre = 'action'
AND
quality = 'HD'

The reason is: The only term that has taxonomy genre equals to action, is action. And the only term that has taxonomy quality equals to HD, is HD. If you constrain them with AND, then basically you have nothing. Since there's no term that satisfies both.

However, you can get movie custom posts that satisfies that criteria. Which means, you need movies with action genre and HD quality.

If that is the case, then you may use the tax_query argument in WP_Query:

$args = array(
    'post_type' => 'movie',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'genre',
            'field'    => 'slug',
            'terms'    => 'action',
        ),
        array(
            'taxonomy' => 'quality',
            'field'    => 'slug',
            'terms'    => 'HD',
        ),
    )
);
echo '<h1>Movies with action genre and HD quality:</h1>';
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    }
    echo '</ul>';
} else {
    echo '<h2>Nothing is found</h2>';
}

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.