Shortcut Args for Display Posts

Featured in: Create Shortcut Arguments for Display Posts Shortcode

<?php
/**
* Industry and Product Category parameters on Display Posts Shortcode
* @author Bill Erickson
* @link http://www.billerickson.net/shortcut-args-for-display-posts-shortcode
*
* @param array $args, WP Query arguments
* @param array $atts, shortcode arguments
* @return array $args
*/
function be_dps_industry_and_category( $args, $atts ) {
// If neither of my custom parameters are in use, return the $args (no modifications necessary)
if( ! ( isset( $atts['industry'] ) || isset( $atts['product_cat'] ) ) )
return $args;
// Set up a tax query, using the existing tax query if there is one
$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
// If they specified an industry, include that in the tax query
if( isset( $atts['industry'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product-industry',
'field' => 'slug',
'terms' => explode( ', ', $atts['industry'] ),
);
}
// If they specified a product category, include that in the tax query
if( isset( $atts['product_cat'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product-category',
'field' => 'slug',
'terms' => explode( ', ', $atts['product_cat'] ),
);
}
// For Multiple tax queries, ensure results match both queries
if( 1 < count( $tax_query ) && !isset( $tax_query['relation'] ) )
$tax_query['relation'] = 'AND';
$args['tax_query'] = $tax_query;
// Set the post type to 'products'
$args['post_type'] = 'products';
return $args;
}
add_filter( 'display_posts_shortcode_args', 'be_dps_industry_and_category', 10, 2 );

Display Posts Shortcode

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk