Title: Change posts_per_page inside loop?
Last modified: August 20, 2016

---

# Change posts_per_page inside loop?

 *  [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/)
 * Hi there,
 * I’ve got a problem I can’t seem to solve…
    On a page displaying posts from a 
   custom post type called ‘news-item’, I want to change the number of posts displayed
   on page 2 and further. This because I want the newest (first) post displayed 
   bigger then the rest, but therefore the layout/grid has to change too. So for
   example:
 * **On Page 1:**
    1 Large post 4 normal posts ————— 5 total
 * **On Page 2:**
    6 normal posts ————— 6 total
 * The part that the first post has to be bigger on page 1 works, but I can’t seem
   to change/set/update the **posts_per_page** parameter inside the loop in order
   to add it with 1. Is that even possible without making a second wp_query?
 * I tried it with:
    `set_query_var( 'posts_per_page', 4 );` and: `$args -> set('
   posts_per_page', 4 );` but that’s not working and messing up my pagination.
 * My code: [http://pastebin.com/hR6K4Wtj](http://pastebin.com/hR6K4Wtj)
 * ANY help is welcome!
    Many thanks!

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/page/2/?output_format=md)

 *  [Mitchell Bundy](https://wordpress.org/support/users/mitchbundy/)
 * (@mitchbundy)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277043)
 * Howdy
    You can’t change the query within the loop. I would suggest have a conditional
   before your arguments manipulating posts_per_page (or `$maxposts`) depending 
   on what `$pageNumber` is.
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277052)
 * Hi there,
    Thanks for your reply! You mean like this:
 *     ```
       if($paged) :
       	$maxposts = 4;
       else:
       	$maxposts = 3;
       endif;
       ```
   
 * I tried that allready and it works sort of however somehow post nr. 5 dissapears?
 *  [Mitchell Bundy](https://wordpress.org/support/users/mitchbundy/)
 * (@mitchbundy)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277056)
 * That’s because the query thought you were displaying 4 posts on the first page,
   you’ll need to use the `offset` parameter as well.
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277068)
 * Thanks for that, I changed it to:
 *     ```
       if($paged) :
       	$maxposts = 4;
       	if ($pageNumber == 2):
       		$offset = 3;
       	endif;
       else:
       	$maxposts = 3;
       endif;
       ```
   
 * And that’s working! But now the pagination is messed up.
    I have 8 posts in total.
   On page 1 the **wp_paginate** shows that there are 3 pages (which is correct 
   3+4+1=8), but on page 2 it shows there are only 2 pages. When I try to access**
   page/3/** there’s no post in it… Any thoughts?
 *  [Mitchell Bundy](https://wordpress.org/support/users/mitchbundy/)
 * (@mitchbundy)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277073)
 * Try:
 *     ```
       if($paged) :
       	$maxposts = 4;
       	$offset = 3;
       else:
       	$maxposts = 3;
       endif;
       ```
   
 * The offset should be used on every page except the first. Let me know how it 
   works out.
 *  [Mitchell Bundy](https://wordpress.org/support/users/mitchbundy/)
 * (@mitchbundy)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277075)
 * Sorry, this:
 *     ```
       if($paged > 1) :
       	$maxposts = 4;
       	$offset = 3;
       else:
       	$maxposts = 3;
       endif;
       ```
   
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277080)
 * Nope, same story… Page 3 doesn’t show up on page 2.
    But **page/3/** shows the
   same posts as page 2 now.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277084)
 * Try using the [is_paged conditional](http://codex.wordpress.org/Conditional_Tags#A_Paged_Page)
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277092)
 * Tried it, but same result as:
    `if($paged > 1)` Keep them coming 🙂
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277110)
 * maybe this will help: [http://wordpress.org/support/topic/different-post-amounts-shown-per-pagination-page](http://wordpress.org/support/topic/different-post-amounts-shown-per-pagination-page)
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277111)
 * Something like:
 *     ```
       if( !is_paged() ) $maxposts = 3;
       wlse $maxposts = 4;
       $args = array(
       	'posts_per_page' => $maxposts,
       	'post_type' => 'news-item',
       	'paged' => $paged
       );
       ```
   
 * should work.
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277127)
 * Thanks both of you!
    My weekend just started so not able to try it out right 
   now… Will try your suggestions as soon as I can! Thanks
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277271)
 * Hi esmi,
    Your solution has the same result as my first attempt: post #5 dissapears…
   I’m gonna try keesiemeijer’s suggestion now, looks promising Thanks!
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277306)
 * Hi keesiemeijer,
 * Different code, same result unfortunately… But in this case the `wp_paginate();`
   doesn’t work anymore. It doesn’t show any pagination, when I replace it with:
 *     ```
       <?php next_posts_link('Older Entries »', 0); ?>
       <?php previous_posts_link('« Newer Entries', 0); ?>
       ```
   
 * I get the same error as in my code; on page 2 the link to page 3 isn’t there…
   I don’t get it anymore.
 * My code: [http://pastebin.com/SHqS7nFZ](http://pastebin.com/SHqS7nFZ)
 * Any other suggestions?
    Thanks!
 *  Thread Starter [Twansparant](https://wordpress.org/support/users/twansparant/)
 * (@twansparant)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/#post-2277310)
 * Found a solution/workaround!
    I modified my code a bit in order to let the `wp_paginate()`
   function show the right amount of pages. The function can pass the total amount
   of pages and the current page as variables. However, if you pass ‘pages’ the 
   highlighting of the current page stops working so you have to pass ‘page’ as 
   well.
 * My final code: [http://pastebin.com/51r7PTwb](http://pastebin.com/51r7PTwb)

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/page/2/?output_format=md)

The topic ‘Change posts_per_page inside loop?’ is closed to new replies.

## Tags

 * [First post](https://wordpress.org/support/topic-tag/first-post/)
 * [pagination](https://wordpress.org/support/topic-tag/pagination/)
 * [posts_per_page](https://wordpress.org/support/topic-tag/posts_per_page/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 5 participants
 * Last reply from: [Yonyoh](https://wordpress.org/support/users/yonyoh/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/change-posts_per_page-inside-loop/page/2/#post-2277367)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
