0

I have multiple loops using wp_query() in WordPress. I want to show the posts from first loop first (post per page=10), and if there are no posts from first loop show, then show the second loop.

For example, if I only have 8 posts from first loop, the second loop should show 2 posts.

The loop is working properly, but I can't solve the post per page issue. How can I do this? I also need pagination for the remaining posts.

$args = array(
                'post_type'         => 'event',
                'event-categories'  => 'featured',
                'orderby'           =>  'meta_value_num', 
                'order'             =>  'ASC',

        );

$loop = new WP_Query( $args );
$args1 = array(
            'post_type'         => 'event',
            'event-categories'  => 'abc'
            'orderby'            =>  'meta_value_num', 
             'order'             =>  'ASC',

        );

$loop1 = new WP_Query( $args1);
5
  • 1
    You say you want to show the second loop if there are no posts in the first; but then, in your example, you have a loop with 8 posts, and you want to show the second with 2 posts. This seems contradictory. Do you mean "if there are less than 10 posts in the first loop, show post from the second loop, up to a maximum of 10 posts in total (loop 1 + loop 2)", i.e. if loop 1 = 3, then loop 2 = 7, if loop 1 = 1, then loop 2 = 9, etc.?
    – markcbain
    Commented May 24, 2014 at 6:59
  • i mean if there is no enough posts to full the page, ie; i need 10 posts in a page. u r correct. Commented May 24, 2014 at 7:01
  • So: posts_per_page (loop 2) = 10 minus posts_per_page (loop 1)? I don't have time right now to submit a full answer, but I would start by using get_query_var to find the number of posts returned for each loop and then doing a calculation to find the correct value for loop 2 posts_per_page.
    – markcbain
    Commented May 24, 2014 at 7:06
  • (y) how can i do that Commented May 24, 2014 at 7:08
  • You could maybe adapt the answer to this question to get the post count wordpress.org/support/topic/…
    – markcbain
    Commented May 24, 2014 at 7:14

1 Answer 1

0

I would do something like this:

<?php

    //Protect against arbitrary paged values
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

    $args = array(
        'posts_per_page' => 6,
        'category_name' => 'case-study',
        'paged' => $paged,
        'show_all'     => False,
        'prev_next'    => True

    );

    $the_query = new WP_Query( $args );
?>


<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

//Insert your data feed here - links / images / text etc etc

 <?php endwhile; ?>

Pagination code below:

<?php

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'prev_next' => 'true',
        'type' => 'list',
        'link_before' => '',
        'link_after'  => '',
        'prev_text'    => __('< FIRST'),
        'next_text'    => __('LAST >'),
        'show_all' => 'true',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );

 ?>

 <?php endif; ?>

Hope this helps :)

6
  • i have two loops. hope u hav read question carefully Commented May 24, 2014 at 7:42
  • Hey yes I understand that. You can place your ARGS when mine are and it should be fine. Try it out and let me know how you get on :)
    – Tired_Man
    Commented May 24, 2014 at 8:05
  • @NishamMahsin do you need to remove duplicate posts from the second loop?
    – markcbain
    Commented May 24, 2014 at 9:53
  • i have done that. but that is not my proble. i hop u understood.thnx for help Commented May 24, 2014 at 9:55
  • wordpress.stackexchange.com/questions/47259/… i have done pagination like this. but only second loop is changing Commented May 24, 2014 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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