Create Shortcut Arguments for Display Posts Shortcode

I might be biased, but I really enjoy using my Display Posts Shortcode plugin. It makes it easy to query posts without any code. It provides clients the ability to dynamically list whatever content they’d like without having me build custom templates. It’s also incredibly powerful – almost all of the WP_Query arguments are available. See for yourself.

But this power leads to an obvious issue – complicated queries require complicated shortcodes. I’m working on a website right now that has a ‘products’ post type and two taxonomies: ‘product-industry’ and ‘product-category’. On industry pages, they want to display products that are in that industry. Seems simple enough, but you end up with a shortcode like this:

[display-posts post_type="products" taxonomy="product-industry" tax_term="agriculture"]

Which makes sense to a developer, but is easy for a non-technical person to type wrong. Lots of hypens, underscores… Am I supposed to use industry, product_industry, product-industry…

Even more difficult is displaying the intersection of two taxonomies. Let’s say we want all the products in the ‘agriculture’ industry and ‘electronics’ category. Here’s the shortcode you’d need:

[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

You can place whatever parameters you’d like in the shortcode. You can then write a function that filters the Display Posts query arguments and leverages those custom parameters. Just be careful when modifying the arguments so that you don’t accidentally affect other uses of the shortcode throughout your site.

In this case, I’m adding two parameters: industry and product_cat. If either of those are provided, I set up the taxonomy query and set the post type to ‘products’. Here’s the code.

<?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 );

First I check to see if my parameters are in use. If they aren’t return the arguments before any modifications are made. Then I check to see if there’s already a taxonomy query, so that I don’t overwrite that one. Then I look at each parameter, and if it’s present I set up a tax query. If there’s multiple tax queries, I set the ‘tax_relation’ to AND so that it will find posts matching both (set to OR if you want to match one or the other). Finally, I overwrite the shortcode’s tax_query with our new one and set the post type to ‘products’.

The first shortcode above can now be rewritten like this:

[display-posts industry="agriculture"]

And the second shortcode like this:

[display-posts industry="agriculture" product_cat="electronics"]

For more examples of customizing this plugin, see my Display Posts Shortcode code snippets.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk

Reader Interactions

Comments are closed. Continue the conversation with me on Twitter: @billerickson

Comments

  1. Laura says

    Ack, sorry! Looking through the docs, now I see I need to add more arguments to the short code, like image_size=”thumbnail” to get the pictures to show up.

    • Bill Erickson says

      You’ll need to check what value is actually being saved to the be_featured meta key. When editing a post you’ve marked featured, click “Screen Options” in the top right corner and make sure “Custom Fields” is checked. Then scroll down to the Custom Fields metabox.

      Look for the be_featured key and note the value that’s saved. You’ll want to update the code you linked to above to match that value.

      For instance, if your plugin is setting the be_featured field to on, update the code to this: https://gist.github.com/billerickson/44db3a4a1a1fe1c52e32ee2752c06d77