pre get posts - pre_get_posts for two loops on same page

admin2025-01-07  8

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?

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?

Share Improve this question edited Sep 18, 2015 at 17:23 user2093301 asked Sep 18, 2015 at 15:06 user2093301user2093301 16910 bronze badges 2
  • 1 Don't use a custom query, I really believe you can do everything you need to with one main query loop – Pieter Goosen Commented Sep 18, 2015 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 Commented Sep 18, 2015 at 15:23
Add a comment  | 

1 Answer 1

Reset to default 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();
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256510a366.html

最新回复(0)