admin管理员组文章数量:1323023
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-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.
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-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.
Share Improve this question edited Sep 18, 2020 at 20:41 panaeleous asked Sep 17, 2020 at 23:12 panaeleouspanaeleous 32 bronze badges1 Answer
Reset to default 0Yes, you're on the right track. If the four taxonomies you mention are the slugs with which the taxonomies are registered, then you need to use those as the taxonomy
parameters and the terms passed with them as the terms
parameters for the different tax_query
arrays.
You'll also need to put the tax parameters to the shortcode_atts
default array to have them available on the $a
variable.
$a = shortcode_atts( array(
'cpt' => '',
'language' => '',
'document_category' => '',
'media_industry' => '',
'product_line' => '', // add default values to these if needed or set defaults as in the example below
), $atts );
The different taxonomies could also be optional,
if ( $a['language'] ) {
$args['tax_query'][] = array(
'taxonomy' => 'media_language',
'field' => 'slug',
'terms' => $a['language'],
);
}
Set default value, if not set with shortcode_atts()
$args['tax_query'] = array(
array(
'taxonomy' => 'media_language',
'field' => 'slug',
'terms' => ! empty($a['language']) ? $a['language'] : 'some-default-term',
),
// more taxonomies...
);
I think 'relation' => 'AND'
is the default, so you might not need to explicitly declare it.
本文标签: shortcodeWP Query by 4 different taxonomies
版权声明:本文标题:shortcode - WP Query by 4 different taxonomies 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742111029a2421255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论