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. Kate says

    I recently downloaded your plugin to displays posts under tags. I’m trying to get it to look like this: http://www.abeautifulmess.com/photography/

    Right now I have a square thumbnail with the post title, which is awesome, but they’re displayed in a list. Is there a way I can display it in rows/columns?

    🙂 Thank you!

    • Bill Erickson says

      You’ll need to use CSS to style it the way you want. The unordered list has a class of .display-posts-listing so you can use that to only target the shortcode’s output with your CSS changes.

  2. laura says

    Hi, I was wondering if the display posts plugin supports proper pagination when calling blog posts from one particular category?

    cheers,

    laura

    • Bill Erickson says

      No, Display Posts Shortcode does not support pagination for a reason. Pagination belongs to the main query on a page. If you need to paginate results, you shouldn’t use the shortcode – you should use one of the built-in WordPress templates instead (ex: category archive page).

  3. Ken says

    Hey, I love your plugin. THanks!
    Currently displaying itineraries like this
    [display-posts post_type="itinerary" taxonomy="itinerary-category" tax_term="cats"]

    Is it possible to display the advanced custom fields assigned to that post type instead of just the linked titles? I wanted to include the description and an image (two custom fields)

    Thanks!
    -ken

  4. Candace Cohen says

    Hi,

    I love your plugin. It literally saved my ass in meeting my deadline. Is there a way to string together more than one argument. For example, I need the current users post to show but I need the full content instead of the list.

    Thanks
    Candace

    • Bill Erickson says

      Yes, you can add as many parameters as you like to the shortcode. Ex: [display-posts author="current" include_content="true"]

  5. Sanchita says

    Hi,
    I am using this plugin for my “Property Rentals” mobile app. I want to display some “meta_key” values like: price, country etc. with the property title below the image.
    Also want to display the property-images of same size and properly aligned for a mobile app.
    Is there any way to do this?

  6. Dan says

    How would I implement a select menu filter so that a user can filter the display based on the selected parameter?

    • Bill Erickson says

      I recommend creating a form that reloads the page, passing a $_GET variable with whatever parameter you want. Then use the information in the article above to use that $_GET variable in adjusting the output of the shortcode.

  7. Mel says

    Hi Bill,

    I really love your neat and nice plugin. Good job! But now I have a little question:

    I want to use the plugin for displaying woocommerce products. Therefore I use the shortcode post_type=”product” and everything is perfect. But when I want to display just one specific products category, it won´t work. (I tried category=”mycatname” and also taxonomy=”category” tax_term=”mycatname”).

    What am I doing wrong?

    Thanks a lot in advance for your answer and best wishes from Germany

    Mel

    • Bill Erickson says

      WooCommerce product categories are a custom taxonomy named product_cat. So you should use this shortcode instead: [display-posts taxonomy="product_cat" tax_term="mycatname"]

  8. Laura Baird says

    Hi, Bill, thank you! This plugin is exactly what I’ve been looking for. However, I tried doing what you suggested above for Woocommerce but it doesn’t seem to be working. I tried it on a clean install of the current WP (4.7.2) with just the plugins Woocommerce and Display Posts Shortcode. I’m using Twenty Fifteen theme.
    http://phillyweb.net/dpshortcode
    It’s supposed to display only animals on the animals page but it’s not displaying anything. (Same with the plants page.)
    I’m wondering if there is a conflict with Woocommerce. Please let me know if you want to access the backend and I’ll send you the username/password.
    Thank you!
    Laura

    • Bill Erickson says

      What is the specific shortcode you’re using, and have you made any code changes (like what this blog post describes)?

      • Laura Baird says

        Thanks, Bill. I’m using:
        [display-posts taxonomy="product_cat" tax_term="animals"]
        I have not made any code changes. It’s just a standard WordPress install with WooCommerce and Display Posts Shortcode.

        • Bill Erickson says

          Change your shortcode to this: [display-posts post_type="product" taxonomy="product_cat" tax_term="animals"]

          The shortcode’s default post type is ‘post’, so your current shortcode is looking for posts in the “animals” term of the “product_cat” taxonomy – and there are none.

    • Laura Baird says

      I meant to say that it displays a list of products as text links only, not along with the thumbnail, name, etc.

    • Bill Erickson says

      Yes, this plugin does not add styling, and does not leverage your existing theme for markup/features.

      You can include a thumbnail with [display-posts image_size="thumbnail"] or whatever image size you want. Styling will need to be done in your theme’s CSS file.

      You can customize the output of the shortcode using the built-in filters. See these code snippets as examples: https://www.billerickson.net/code-tag/display-posts-shortcode/