Question

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.

Was it helpful?

Solution

Yes, 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top