WP Query by 4 different taxonomies
I need to make a shortcode that uses the values of four different taxonomies. I am trying to modify code that I have used 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
- mp_industry
- mp_product_lines
I want to use a shortcode like :
[get_media_by_taxes cpt=attachment media_language=spanish media_document_category=brochure mp_industry=food mp_product_lines=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',
'media_language' = 'english',
'media_document_category' = 'brochures',
'mp_industry' = 'food',
'mp_product_lines' = 'allergens', // add default values to these if needed or set defaults as in the example below
), $atts );
$args = array(
'post_status' = 'inherit',
'posts_per_page' = -1,
'post_type' = $a['cpt'],
);
$terms = explode(',', $a['terms']);
$args['tax_query'] = array(
array(
'taxonomy' = 'media_language',
'field' = $terms,
'terms' = ! empty($a['language']) ? $a['language'] : 'english',
),
array(
'taxonomy' = 'media_document_category',
'field' = $terms,
'terms' = ! empty($a['document_category']) ? $a['document_category'] : 'brochures',
),
array(
'taxonomy' = 'mp_industry',
'field' = $terms,
'terms' = ! empty($a['industry']) ? $a['industry'] : 'food',
),
array(
'taxonomy' = 'mp_product_lines',
'field' = $terms,
'terms' = ! empty($a['product_lines']) ? $a['product_lines'] : 'allergens',
),
);
$output='div class=media-attachments-listul';
$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.