Title: Using query_posts within a shortcode
Last modified: August 20, 2016

---

# Using query_posts within a shortcode

 *  Resolved [EddJB](https://wordpress.org/support/users/eddjb/)
 * (@eddjb)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/using-query_posts-within-a-shortcode/)
 * [Apologies if in the wrong section and sorry if this is a really stupid question]
 * I’ve defined a custom post type. The featured images of this custom post type
   are used as a carousel. That all works fine and using cycle.js with the following
   code the carousel works.
 *     ```
       <div class="carousel">
       <?php $temp_query = clone($wp_query); ?>
       <?php query_posts('post_type=carousel&showposts=10'); ?>
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
       <div class="slide"><li>
               <?php if(has_post_thumbnail())
       		{
       		the_post_thumbnail('featured_image');
       		} else {
       			 echo '<img width="460" height="280" src="image/placeholder.jpg" class="attachment-featured_image wp-post-image">'; }
       			?></li> </div><!-- Closing the class slide -->
       	<?php endwhile; endif; ?>
          <?php wp_reset_query(); ?>
       </div>
       ```
   
 * I’ve been trying to work out a way to insert the above code into a short-code
   so that I can type [carousel] anywhere on the site and the carousel as defined
   above will appear rather than having to hardcode it in to a page. I’ve used shortcodes
   before to define basic layout stuff using the return function but I can’t work
   out how I can introduce PHP tp them in this way.
 * I’d be really grateful for any help anyone can offer.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/using-query_posts-within-a-shortcode/#post-3601784)
 * start by reviewing [http://codex.wordpress.org/Shortcode_API](http://codex.wordpress.org/Shortcode_API)
 * general:
 * shortcodes have to _**return**_ the results instead of _**echo**_ing it; this
   will require recoding of your loop;
 * also, using `query_posts()` in secondary loops is not recommended; use `WP_Query()`
   instead;
 * also, there is a problem with your provided code as the usage of the `div` and`
   li` elements will cause invalid html code;
    as this has to work with the script
   for the carousel, I can’t say for sure what you need to change there. the below
   is based on your existing code:
 * example for the loop only (please have a go and try to build the rest of the 
   shortcode yourself):
 *     ```
       $output = '';
       $carousel_query = new WP_Query('post_type=carousel&showposts=10');
       if ($carousel_query->have_posts()) :
       $output .= '<div class="carousel">';
       while ($carousel_query->have_posts()) : $carousel_query->the_post();
       $output .= '<div class="slide"><li>';
         if(has_post_thumbnail())
       	{
         $output .= get_the_post_thumbnail($post->ID,'featured_image');
       	} else {
         $output .= '<img width="460" height="280" src="image/placeholder.jpg" class="attachment-featured_image wp-post-image" alt="" />'; }
       $output .= '</li></div><!-- Closing the class slide -->';
       endwhile;
       $output .= '</div>';
       endif;
       wp_reset_postdata(); 
   
       return $output;
       ```
   
 * (fully untested; subject to typing mistakes)
 *  Thread Starter [EddJB](https://wordpress.org/support/users/eddjb/)
 * (@eddjb)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/using-query_posts-within-a-shortcode/#post-3601788)
 * Thanks alot. Stupidly I hadn’t yet read the Shortcode API, which would have been
   a good starting point. Many thanks for your help! Hopefully I’ll be able to work
   it out from here.
 *  Thread Starter [EddJB](https://wordpress.org/support/users/eddjb/)
 * (@eddjb)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/using-query_posts-within-a-shortcode/#post-3601797)
 * For anyone searching for this, Alchymth’s solution worked almost immediately.
   I had to remove my own mistake of the incorrect
    `<li>` tag. Beyond that, the
   only slightly strange thing was that the `<!-- Closing the class slide -->` added
   a <p> tag which cycle.js treated as another slide. Once removed it all worked
   perfectly.
 * Full code was
 *     ```
       <?php
       function l_carousel( $atts, $content = null ) {
   
       $output = '';
       $carousel_query = new WP_Query('post_type=carousel&showposts=10');
       if ($carousel_query->have_posts()) :
       $output .= '<div class="carousel">';
       while ($carousel_query->have_posts()) : $carousel_query->the_post();
       $output .= '<div class="slide">';
         if(has_post_thumbnail())
       	{
         $output .= get_the_post_thumbnail($post->ID,'featured_image');
       	} else {
         $output .= '<img width="460" height="280" src="/image/placeholder.jpg" class="attachment-featured_image wp-post-image" alt="" />'; }
       $output .= '</div>';
   
       endwhile;
       $output .= '</div>';
       endif;
       wp_reset_postdata(); 
   
       return $output;
   
       }
   
       add_shortcode( 'carousel', 'l_carousel' ); ?>
       ```
   
 * Thanks to Alchymyth again!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Using query_posts within a shortcode’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [short codes](https://wordpress.org/support/topic-tag/short-codes/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [EddJB](https://wordpress.org/support/users/eddjb/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/using-query_posts-within-a-shortcode/#post-3601797)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
