I want to make a shortcode like this [myshorcode:2:drinks], and query-wrangler I have managed to get the following query that shows me 2 products of a certain taxonomy

PHP WP_Query:

$query = array (
  'paged' => 1,
  'posts_per_page' => '2',
  'offset' => 0,
  'post_status' => 'publish',
  'ignore_sticky_posts' => 0,
  'orderby' => 'date',
  'order' => 'DESC',
  'tax_query' => 
  array (
    0 => 
    array (
      'taxonomy' => 'product_brand',
      'field' => 'id',
      'terms' => 
      array (
        0 => 30,
      'operator' => 'IN',
      'include_children' => false,
  'post_type' => 
  array (
    'product' => 'product',

This query works well but doesn't give me the option to put the name of the taxonomy, just the id.

That's why I would like to make a shortcode where I put [product:2:name-taxonomy] or [product:name-taxonomy], and the 2 I leave it fixed.

Thank you for your opinions and help

1 Answer 1


I'm not quite sure what you want to do as frankly, your terminology is a bit scrambled, and you example shortcode is also quite a mess.

Before I start, you should start of by checking the following very important links

You should first start of by defining your attributes (and their defaults) which you need to use and pass to the shortcode. From your example, you can do something like this

$a = shortcode_atts( 
        'posts_per_page' => 2,
        'post_type' => 'product',
        'taxonomy'   => 'product_brand', 
        'term_id'  => '',
        'term_name'  => '',
        'term_slug'  => '',
        // Rest of attributes required
$atts ); 

You can set as many attributes as needed to suite your needs. From what I can understand, you want to pass either the term name or id to the shortcode, so I have set up the attributes accordingly. I have included the slug as well. To keep the shortcode dynamic, it will be best not to hardcode your arguments for the query. Set them as attributes with a default value. If you need to reuse the shortcode for another taxonomy or post type, you can change the values through the attributes passed

The next thing what you need to do is to adjust your tax_query according to the term value passed in order to set the correct field value. For this, you can try a condition like this

$term_id = $a['term_id'];
$term_name = $a['term_name'];
$term_slug = $a['term_slug'];

if ( $term_id ) {
    $terms = $term_id;
    $field = 'term_id';
} elseif ( $term_name ) {
    $terms = $term_name;
    $field = 'name';
} elseif ( $term_slug ) {
    $terms = $term_slug;
    $field = 'slug';

You can now just pass $terms and $field to your tax_query

'tax_query' => array(
        'taxonomy' => $a['taxonomy'],
        'field' => $field,
        'terms' => $terms,
        'include_children' => false

You can now pass the term id, name or slug as required in your shortcode as follows

[my_shortcode posts_per_page='3' term_id=3]


[my_shortcode posts_per_page='3' term_name='My term name']


[my_shortcode posts_per_page='3' term_slug='my-term-slug']


  • This is a just a skeleton to set you on your way. Expand and abuse it as needed

  • You would want to sanitize and validate data coming from your attributes accordingly to avoid possible bugs, unexpected output and security loopholes. For instance, you would maybe first check if you have a term value and then checking if that term actually exists before passing it as an argument and building your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.