I'm in the process of developing a plugin with a custom page template (using template_include) with a loop that retrieves all posts with a custom taxonomy. I tested the code using the same templates files directly in the theme folder, the loop works as it should.

However, as a plugin, the page template fails to retrieve all posts. It only returns a single post. I've included the relevant loop code below:

        // Retrieve all terms for the custom tax
        $helpterms = get_terms('ways_to_help');

        // Use wp_list_pluck to retrieve only the IDs for the terms
        $termID = wp_list_pluck( $helpterms, 'term_id' );

        $loop = new WP_Query( array( 
            'post_type' => 'post', 
            'orderby' => 'rand', 
            'posts_per_page' => -1,
            'tax_query' => array(
                            'taxonomy' => 'ways_to_help',
                            'field' => 'term_id',
                            'terms' => $termID
<div id="ways-to-help">
  <h2>All Ways To Help</h2>
    <div class="help-container">
        while ( $loop->have_posts() ) : $loop->the_post();
            // Using include_once instead of get_template_part for plugin
            include_once( 'content-ways-to-help.php' );
           // get_template_part( 'content-ways-to-help', get_post_format() );

      <?php endwhile; ?>
<?php fistulaf_social_share(); ?>

Now this same code works totally fine in the theme folder, retrieving all of the posts in the custom tax. I can't figure out why it's not retrieving the correct number of posts. The posts_per_page => -1 should retrieve them all?

As per others suggestions I've included the plugin code for including the page template here as a gist: https://gist.github.com/tnog/9374375504a6cdfcf0af

Appreciate help from anyone who could elucidate this strange issue. Thanks!

  • Please post the code that you use to include the page template with – Pieter Goosen Aug 3 '15 at 7:32
  • I just updated my question with a gist with the plugin code for including the page template. – tnog Aug 3 '15 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.