1

I'm using two loops on homepage, one is to show the 3 latest posts, and the other one to show the rest of the posts minus the 3 first posts.

I'm using the following code for the pre_get_posts.

function tax_and_offset_homepage( $query ) {
 if ( !is_admin() && $query->is_home() && $query->is_main_query() )
  $query->set( 'post_type', array( 'post') );

$ppp = get_option( 'posts_per_page' );
//$ppp = 300;
$offset = 3;

if ( !$query->is_paged() ) {
  $query->set( 'posts_per_page', $ppp);
        $query->set( 'offset', $offset);

 }
} 
add_action('pre_get_posts','tax_and_offset_homepage');

and the following code for the basic loop:

<?php 
 if ( have_posts() ) {
while ( have_posts() ) {
    the_post(); 
    //
    // Post Content here
    //
} // end while
} // end if
?>

but the problem is that the pre_get_posts can be applied to one of the 2 loops. How can i have 2 pre_get_posts function for two loops on same page?

  • 1
    Don't use a custom query, I really believe you can do everything you need to with one main query loop – Pieter Goosen Sep 18 '15 at 15:09
  • If you have a custom query, why would you need pre_get_posts? It's better to use that filter on the main query, or all queries, it doesn't make as much sense to modify custom loops with it. It's better to pass the arguments through a custom filter in that scenario – Tom J Nowell Sep 18 '15 at 15:23
0

Have a look at WP_Query when you need to loop through posts.

<?php 
  $args = array( 
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'post__not_in' => array(),
    'order' => 'DESC',
  );
  $the_query = new WP_Query($args);

  // Show the first 3 posts
  while($the_query->have_posts()) { $the_query->the_post(); 
    echo '<h2>'.get_the_title().'</h2>';

    // Have we shown 3 yet? Break then. (current_post counter stats at 0)
    if( $the_query->current_post == 2 ) { break; }
  }

  // Continue the loop where we left off
  while($the_query->have_posts()) { $the_query->the_post(); 
    echo '<h2>'.get_the_title().'</h2>';
  }

  wp_reset_postdata();
?>
  • thanks for this. the only problem though is that i've split the 2 loops because on the second one i am using a for($count=0;have_posts();$count++).... and it is confusing as it is now :D – user2093301 Sep 18 '15 at 18:46
  • You've lost me. – deflime Sep 18 '15 at 18:47
  • @user2093301 please reread my comment to your answer, in short, your setup is a mess which in turn is causing the issue you are having, remove the custom queries, use the main query and then manipulate your output from there. It is that easy – Pieter Goosen Sep 18 '15 at 18:56
  • will do! :) any ideas how to implement this counter though? for($count=0;have_posts();$count++) : the_post(); $open = !($count%5) ? '<ul class="promo content-over">' : ''; //Create open wrapper if count is divisible by 3 $close = !($count%5) && $count ? '</ul>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0 echo $close.$open; – user2093301 Sep 18 '15 at 19:07
  • 1
    Query objects have a built in counter - $the_query->current_post. Note that it starts at zero. – Milo Sep 18 '15 at 19:10

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.