I might be biased, but I really enjoy using my Display Posts Shortcode plugin. It simplifies querying posts without writing PHP and gives clients an easy way to list content dynamically without custom templates. It’s also powerful — most WP_Query arguments are supported.
That power does introduce a downside: complex queries lead to long, error-prone shortcodes. On a project with a custom post type named “products” and two taxonomies, “product-industry” and “product-category”, the straightforward developer-style shortcode for showing products in an industry looks like this:
[display-posts post_type="products" taxonomy="product-industry" tax_term="agriculture"]
That works, but it’s easy for non-technical users to mistype. Hyphens, underscores and different naming conventions make it unclear whether to use industry, product_industry, or product-industry.
Displaying the intersection of two taxonomies is even more cumbersome. To get products in the “agriculture” industry and the “electronics” category you would need a shortcode like:
[display-posts post_type="products" taxonomy="product-industry" tax_term="agriculture" taxonomy_2="product-category" tax_2_term="electronics" tax_relation="AND"]
Creating Shortcut Arguments
Fortunately, the Display Posts Shortcode supports custom parameters. By adding simple, human-friendly attributes to the shortcode and filtering the plugin’s query arguments, you can make shortcodes easier to type and understand. Just take care to modify arguments only when your custom parameters are present so other shortcodes aren’t affected.
In this example I add two shortcut attributes: industry and product_cat. When either is supplied, the filter builds a tax_query for the relevant taxonomy and forces the post type to products. The code below shows the complete filter implementation.
| /** |
| * Industry and Product Category parameters on Display Posts Shortcode |
| * @author Bill Erickson |
| * |
| * @param array $args WP Query arguments |
| * @param array $atts shortcode attributes |
| * @return array $args |
| */ |
| function be_dps_industry_and_category( $args, $atts ) { |
| // If neither custom parameter is present, return unmodified args |
| if ( ! ( isset( $atts[‘industry’] ) || isset( $atts[‘product_cat’] ) ) ) |
| return $args; |
| // Preserve any existing tax_query |
| $tax_query = isset( $args[‘tax_query’] ) ? $args[‘tax_query’] : array(); |
| // Add industry term(s) if provided |
| if ( isset( $atts[‘industry’] ) ) { |
| $tax_query[] = array( |
| ‘taxonomy’ => ‘product-industry’, |
| ‘field’ => ‘slug’, |
| ‘terms’ => explode( ‘, ‘, $atts[‘industry’] ), |
| ); |
| } |
| // Add product category term(s) if provided |
| if ( isset( $atts[‘product_cat’] ) ) { |
| $tax_query[] = array( |
| ‘taxonomy’ => ‘product-category’, |
| ‘field’ => ‘slug’, |
| ‘terms’ => explode( ‘, ‘, $atts[‘product_cat’] ), |
| ); |
| } |
| // If multiple tax queries exist, require posts to match all of them |
| if ( 1 < count( $tax_query ) && ! isset( $tax_query[‘relation’] ) ) |
| $tax_query[‘relation’] = ‘AND’; |
| $args[‘tax_query’] = $tax_query; |
| // Ensure the post type is products |
| $args[‘post_type’] = ‘products’; |
| return $args; |
| } |
| add_filter( ‘display_posts_shortcode_args’, ‘be_dps_industry_and_category’, 10, 2 ); |
In plain terms: first check whether the friendly parameters are present and skip any changes if they aren’t. If they are present, preserve any existing tax_query, then add a tax_query clause for each supplied parameter. If there’s more than one clause, set the relation to AND so results must match both (use OR if you prefer either/or). Finally, replace the shortcode’s tax_query and set post_type to products.
With this filter in place, the earlier long shortcodes become much simpler:
[display-posts industry="agriculture"]
And for the intersection of industry and category:
[display-posts industry="agriculture" product_cat="electronics"]
This approach keeps shortcodes readable and reduces mistakes while still leveraging the full power of Display Posts Shortcode.