0

I have a query which returns all custom post, I'm trying to split all post into sections of six then wrap them in an list item.

Right now its performing as it should but breaks when it get to the 12 post, their are a total of 40 post. Im not sure what wrong could someone help.

<?php 

        $args = array(
            'post_type' => 'portfolio',
            'posts_per_page' => -1                
        );

$loop = new WP_Query( $args ); while ( $loop->have_posts()) : $loop->the_post(); ?>

<?php if($count === 1 ): echo "<li>"; endif; ?>

    <?php if($count % 7 == 0 ): echo "<li>"; endif; ?>
     <div class="portfolio">
        <?php the_post_thumbnail('portfolio-large'); ?>
        <div><?php the_excerpt(); ?></div>
        <a href="<?php the_permalink(); ?>" style="color:#fff;">more info</a>
    </div>

<?php if($count % 6 == 0 ): echo "</li>"; endif; ?>

<?php endwhile; wp_reset_query(); ?>
1
  • The query is present i have just not includes it
    – John Doe
    Jan 20 '14 at 17:27
1

I think your approach is correct but you're running into the "posts_per_page" problem.

In your query, use 'posts_per_page=-1' to return all posts (assuming that's what you want). I'm guessing your website is currently set to show 12 posts per page in the settings->reading area.

http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

0

Could you use the offset property in the query e.g.

 $query = new WP_Query( 'posts_per_page=6' ) );
 $query2 = new WP_Query( 'posts_per_page=6offset=6' ) );

You would basically do 2 queries, 6 posts in query 1, and then in query 2 do the 6 posts again but add an offset of 6, which will skip the first 6 posts so they aren't repeated.

2
  • I will give it a go later this evening
    – John Doe
    Jan 20 '14 at 18:02
  • don't do additional queries for something that can be done entirely in php, you will use far more memory and cpu for each additional query.
    – Milo
    Jan 20 '14 at 19:23
0

I turned out that I had wp_reset_query(); in the middle of my loop which was resitting the offset counter, also I changed

<?php if($count === 1 ): echo "<li>"; endif; ?>
<?php if($count % 7 == 0 ): echo "<li>"; endif; ?>

to <?php if($count % 6 == 0 ): echo "<li>"; endif; ?>

which fixed my issue.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.