Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.