• Resolved livingstonescreative

    (@livingstonescreative)


    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

    (@tw2113)

    The BenchPresser

    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

    (@livingstonescreative)

    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

    (@tw2113)

    The BenchPresser

    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.