1 min read

Show WordPress Posts that belong to two or more custom taxonomies

You can achieve this by using the tax_query parameter in a custom WP_Query loop. Here’s how you can do it:

<?php
// Define the taxonomies you want to query by
$taxonomy_1 = 'taxonomy_name_1';
$taxonomy_2 = 'taxonomy_name_2';

// Define the terms you want to query by
$term_slug_1 = 'term_slug_1';
$term_slug_2 = 'term_slug_2';

// Query arguments
$args = array(
    'post_type' => 'puppies',
    'tax_query' => array(
        'relation' => 'AND', // Set the relation to 'AND' to find posts belonging to both taxonomies
        array(
            'taxonomy' => $taxonomy_1,
            'field' => 'slug',
            'terms' => $term_slug_1,
        ),
        array(
            'taxonomy' => $taxonomy_2,
            'field' => 'slug',
            'terms' => $term_slug_2,
        ),
    ),
);

// The Query
$query = new WP_Query($args);

// The Loop
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        // Your loop content here
    }
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // No posts found
    echo 'No posts found';
}
?>

Replace 'taxonomy_name_1', 'taxonomy_name_2', 'term_slug_1', and 'term_slug_2' with the actual names and slugs of your taxonomies and terms.

This query will find posts that belong to both taxonomies specified in the tax_query array. You can adjust the relation parameter to 'OR' if you want to find posts that belong to either taxonomy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.