Title: wp_query not returning posts
Last modified: September 7, 2018

---

# wp_query not returning posts

 *  Resolved [livingstonescreative](https://wordpress.org/support/users/livingstonescreative/)
 * (@livingstonescreative)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/wp_query-not-returning-posts/)
 * I’m having trouble getting a shortcode to display custom post types. My shortcode
   returns ‘Not Found’ even if I leave the query as vague as just entering the post_type
   and posts_per_page arguments. What am I missing?
 * Here is my shortcode :
 *     ```
       // shortcode to display simple CPT block
       function cpt_type_query($atts, $content = null){
         extract(shortcode_atts(array( 
          'posts_per_page' => '5',
          'post_type' => 'cpt_type'
         ), $atts));
   
         global $post;
   
         $posts = new WP_Query($atts);
         $out = '<p><i>Not Found</i></p>';
         if ($posts->have_posts())
               while ($posts->have_posts()):
                   $posts->the_post();
   
                   $out = '<div class="sf-result-item">
                             <a href="'.get_permalink().'">
                               <h2>'.get_the_title().'</h2>
                               <p>'.get_the_excerpt().'</p>
                             </a>';
                   $out .='</div>';
   
               endwhile;
   
         wp_reset_query();
         return html_entity_decode($out);
       }
       add_shortcode('cpt_type', 'cpt_type_query');
       ```
   

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

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [7 years, 11 months ago](https://wordpress.org/support/topic/wp_query-not-returning-posts/#post-10667332)
 * Honestly, I’d avoid using `$posts` as a variable name and also avoid doing the`
   global $post` part just because of WP’s sometimes wonky use of globals as a whole.
   Also just in case it’s not a “dummy” example, make sure that your `'post_type'`
   attribute is actually ending up being the post type you intend.
 * Lastly, I would check and make sure that your `$atts` variable actually has what
   you need in it. You extract the values, but it’s hard to say if `$atts` actually
   gets populated.
 *  Thread Starter [livingstonescreative](https://wordpress.org/support/users/livingstonescreative/)
 * (@livingstonescreative)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/wp_query-not-returning-posts/#post-10673530)
 * Thanks for the reply. Looks like I misunderstood what the extract function was
   doing. I thought this set default variables if a variable couldn’t be found within
   the $atts, rather than it placing those in variables that could then be used 
   within the function itself.
 * I therefore redrafted the function to make use of those variables and this now
   works fine.
 *     ```
       // shortcode to display simple CPT block
       function cpt_type_query($atts, $content = null){
   
         // define attributes and their defaults
         extract( shortcode_atts( array (
             'slug' => '',
             'per_page' => -1,
         ), $atts ) );
   
         // define query parameters based on those attributes
         $args = array(
             'post_type' => 'cpt_type',
             'order' => 'date',
             'orderby' => 'title',
             'posts_per_page' => $per_page, 
             'name' => $slug,
         ); 
   
         $cpt_posts = new WP_Query($args);  // using $args NOT $atts
         $out = '<p><i>Not Found</i></p>';
   
         if ($cpt_posts->have_posts())
               while ($cpt_posts->have_posts()):
                   $cpt_posts->the_post();
   
                   $out = '<div class="sf-result-item">
                             <a href="'.get_permalink().'">
                               <h2>'.get_the_title().'</h2>
                               <p>'.get_the_excerpt().'</p>
                             </a>';
                   $out .='</div>';
   
               endwhile;
   
         wp_reset_query();
         return html_entity_decode($out);
       }
       add_shortcode('cpt_type', 'cpt_type_query');
       ```
   
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [7 years, 11 months ago](https://wordpress.org/support/topic/wp_query-not-returning-posts/#post-10673629)
 * Awesome to hear that you got it working.

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

The topic ‘wp_query not returning posts’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

## Tags

 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 3 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/wp_query-not-returning-posts/#post-10673629)
 * Status: resolved