Can’t limit posts_per_page in loop

I have a loop that cycles through every tag and displays 10 posts from each. This works, but I want to set it to only show 20 posts on each page overall i.e. page 1 shows tag 1, tag 2, then page 2 shows tag 3, tag 4 etc

I’m not sure how to do this. Do I need to put the foreach loop in another array and define posts_per_page in the new array? Or does it maybe require setting up a function with pre_get_posts?

$paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;

$car_group_terms = get_terms( ‘post_tag’ );

foreach ( $car_group_terms as $car_group_term ) {
$car_group_query = new WP_Query( array(
‘post_type’ => ‘car_listing’,
‘posts_per_page’ => 10,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘post_tag’,
‘field’ => ‘slug’,
‘terms’ => array( $car_group_term->slug ),
‘operator’ => ‘IN’
)
)
) );

while ($car_group_query->have_posts() ) : $car_group_query->the_post();
get_template_part( ‘content’, ‘car_listing’ );
}
endwhile;
}

Read more here:: Can’t limit posts_per_page in loop

Leave a Reply

Your email address will not be published. Required fields are marked *