0

I have a query that is showing the 5 latest posts on my homepage from the blog. After the second post I have added a div #more-news then the posts carry on in order after this. This was all working fine and in order, until I tried to add another query within the div #more-news.

What is happening now is the first two posts load fine, then the div #more-news, which contains the next 5 posts (6,7,8,9,10) after the existing 5, however after adding my second query, rather than the posts continuing in order, its display the page title 'home' as the fourth post then 3rd & 4th post.

To try and explain better what I'm after this may help show the order I'm trying to achieve

[1] [2] [morenews = 6,7,8,9,10]

[3] [4] [5]

<?php $my_query = "showposts=5"; $my_query = new WP_Query($my_query); ?>
                <?php if ($my_query->have_posts()) :?> 
                    <?php $count = 0; ?>
                    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                        <?php $count++; ?>
                        <?php if ($count == 3) : ?>
                            <div id="more-news" class="col-lg-4">
                                <h2>More News</h2>
                                    <?php
                                    $mnquery = new WP_Query(array(
                                        'posts_per_page'   => 5,
                                        'offset'   => 5,

                                    ));
                                    while ($mnquery->have_posts()): $mnquery->the_post(); ?>
                                        <li>
                                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                        </li>
                                    <?php endwhile;?>
                                    <?php wp_reset_postdata(); ?> 
                            </div>
                        <?php endif ?>
                        <div class="col-lg-4">
                            <article <?php post_class('news-post'); ?>>
                                <div class="innerPost">
                                    <a class="postLink" href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail('full', array( 'class' => 'img-responsive')); ?>
                                    </a>
                                    <div class="postDetails">
                                        <div class="postDetInner">
                                            <header>
                                                <div class="arrowpan"></div>
                                            </header>
                                        </div>
                                    </div>
                                </div>
                                <div class="news-post-inner">
                                    <?php 
                                        $category = get_the_category(); 
                                        if($category[0]){
                                            echo '<a class="category-link" href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
                                        }
                                    ?>
                                    <h2 class="entry-title"><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title( $ID ); ?> </a></h2>
                                </div>
                            </article>
                        </div>
                    <?php endwhile; // end of one post ?>
                <?php endif; //end of loop ?>
            <?php wp_reset_postdata(); ?> 
3

We can simplify your code and just run one query instead of two. The answer lies in rewinding your loop and rerunning it multiple times. We will use the build in loop counter, $current_post to count our posts. Just remember, this counter starts at 0, and not 1, so post 1 will be 0

So here is what we need to do

  • Query 10 posts instead of doing two loops of five

  • Run the loop and only display the first two posts

  • Rewind our loop, and rerun it and output posts 6-10

  • Rewind our loop again, and rerun the loop once more and display posts 3-5

In code (skeleton only), you can try something like this: (Requires PHP 5.4+ due to new short array syntax ([]), for older versions, replace with old array syntax (array()))

$args = [
    'posts_per_page' => 10
];
$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    // Run the loop the first time to display the first two posts
    while ( $q->have_posts() ) {
        $q->the_post();

        if ( $q->current_post <= 1 ) {

            // Do what you need to do with the first two posts

        } 
    }

    // Rewind the loop
    $q->rewind_posts();
    // Run the loop the second time to display posts 6 - 10

    while ( $q->have_posts() ) {
        $q->the_post();

        if ( $q->current_post >= 5 && $q->current_post <= 9 ) {

            // Do what you need to do with posts 6 - 10

        } 
    }

    // Rewind the loop
    $q->rewind_posts();

    // Run the loop the third time to display posts 3 - 5
    while ( $q->have_posts() ) {
        $q->the_post();

        if ( $q->current_post >= 2 && $q->current_post <= 4 ) {

            // Do what you need to do with posts 3 - 5

        } 
    }
    wp_reset_postdata();
}
| improve this answer | |
  • Its working fine apart from the posts 6-10. It doesn't print anything. I have changed the number 5 to 4 and it then prints the same title as the [5] post but no more. – user2648610 Jun 14 '15 at 19:32
  • Are you sure you have more than 5 posts, and did you make sure that you changed your code to query 10 posts instead of 5. I cannot see any reason why my code should not work – Pieter Goosen Jun 14 '15 at 19:42
  • thats embarrassing, forgot to change it to 10 :P. Thank you again so much for the help. – user2648610 Jun 14 '15 at 19:47
  • My pleasure, enjoy. Off to bed now :-) – Pieter Goosen Jun 14 '15 at 19:54

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.