WP Query by 4 different taxonomies

Question

I have a shortcode I made to query attachments (in media) based off of multiple taxonomy terms, but what I need is to wp_query based off of 4 (four) taxonomies values. I realize that I am way off, but perhaps somebody can help put me on the right track.

The Four taxonomies are:

  • media_language
  • media_document_category
  • media_industry
  • mp_product_line

I want to use a shortcode like :
[get_media_by_taxes cpt="attachment" language="spanish" document_category="Food" product_line="Allergens"] <— can I do it this way?

I know the relationship between queries taxonomies is “AND”.. below is a copy of the shortcode that pulls by multiple terms.

    function get_media_by_taxes($atts){                             

    $a = shortcode_atts( array(
        'cpt' => 'attachment',
        'tax' => 'category',
        'terms' => 'term'
    ), $atts );

    $args = array(
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'post_type' =>  $a['cpt'],
    );
    
    $terms = explode(',', $a['terms']);


    $args['tax_query'] = array(
        array(
            'taxonomy' => $a['tax'],
            'terms' => $terms,
            'field' => 'slug',
        ),
    );
    
    $output='<div class="media-attachments-list"><ul>';

    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
        $the_query->the_post();
            $output.= '<li>' . wp_get_attachment_link() . '</li>';
        }
    }
    return $output .= '</ul></div>';
    
    /* Restore original Post Data */
    wp_reset_postdata();

}

      add_shortcode('get_media_by_taxes', 'get_media_by_taxes');

Thank you for any help you can offer.

0
panaeleous 4 months 0 Answers 19 views 1

Leave an answer