I am working on a site that will be listing a few hundred products and we wanted to be able to quickly/easily show specific products on a page based on taxonomies. I have been doing some research and thought I was on the right track.

First, I found this article on digwp.com which I thought was going to work great. I can not get it to pull the posts based on taxonomy though - just post_type. The Post Type is "Used Items" and I created a taxonomy named Color where one of the slugs was Red.

[loop the_query="post_type=used-items&color=red&ord=ASC"]

I also found this plugin for doing a query by shortcode, but same situation with it not pulling by taxonomy.

My question Does anyone know what I can do to pull posts based on post type/taxonomies (would be using multiple each query) directly from a WordPress page. We want to be able to quickly query products without having to program the query into a page template for each possible option.

1 Answer 1


here is a simple shortcode that can handle taxonomies, post types and any other parameter that WP_Query takes:

function posts_shortcode_handler($atts, $content){
        'posts_per_page' => '5',
    ), $atts));

    global $post;
    $temp = $post;

    $posts = new WP_Query($atts);
    $retVal = '';
    if ($posts->have_posts()){
        while ($posts->have_posts()){

            // these arguments will be available from inside $content
            $parameters = array(
                'PERMALINK' => get_permalink(),
                'TITLE' => get_the_title(),
                'CONTENT' => get_the_content(),
                'CATEGORIES' => get_the_category_list(', '),
                'THUMBNAIL' => get_the_post_thumbnail()

            $finds = $replaces = array();
            foreach($parameters as $find => $replace){
                $finds[] = '{'.$find.'}';
                $replaces[] = $replace;
            $retVal .= str_replace($finds, $replaces, $content);

    $post = $temp;
    return $retVal;


[posts post_type="page" posts_per_page=5 taxonomy_name="taxonomy_term"]
    <h5><a href="{PERMALINK}">{TITLE}</a></h5>
    <div>{THUMBNAIL} <br />{CONTENT}</div>

replace page with your post type name, taxonomy_name with your taxonomy name and taxonomy_term with the taxonomy term

  • Looks great so far. Two questions that hopefully you can help with. 1). How would I search if I wanted to pull results that contained two taxonomy terms from the same taxonomy name. So only if Red and Blue were selected for the term Color, but it has to be both colors. Make sense? 2). Any way of putting the output code into the function so I only need to put the shortcode in and it displays the output code I want each time (title, link, thumbnail).? Thanks for such a quick response!
    – Adam
    Apr 3, 2012 at 21:59
  • 1) you would need to edit the function to handle multiple taxonomy terms using tax_query and as for the template you can hard-code it in the function and you will never have to enter it again :)
    – Bainternet
    Apr 3, 2012 at 22:10
  • Would you be able to assist me with this? I am not sure where to edit the function for the output. I am looking to have it out the title, thumbnail and have both link to the page. I am also not sure how to setup the 'tax_query'. If you can help me I would really appreciate it, this stuff always gives me trouble :)
    – Adam
    Apr 3, 2012 at 22:25
  • Is this still the best way? Also, how would you adjust the order of the output?
    – Garconis
    Aug 31, 2017 at 18:08
  • Also, this seems to add blank <p></p> tags before and after each item.
    – Garconis
    Aug 31, 2017 at 19:01

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.