Override the default number of posts to show for a single loop?
Question
Is is possible to override the set # of blog posts to show per page (as defined under Reading Settings in the WordPress admin)? I want to make it so a custom loop that I am using will show an unlimited number.
Solution
The argument that controls how many posts are shown in the query is posts_per_page
<?php query_posts( array(
'post_type' => 'post',
'posts_per_page' => -1 )
);
?>
Also to note is that there is a bug in the 3.0 branch which prevents the -1 integer from displaying all posts. It is fixed in 3.1 but a workaround would be to use a very high number instead of -1
see:
OTHER TIPS
Sure, change the query by adding
<?php query_posts('post_type=post&numberposts=-1'); ?>
Eileen is right but it's better to use arguments as an array <?php query_posts( array( 'post_type' => 'post', 'numberposts' => -1 ) ); ?>
I had the same issue.. I decided to add a custom variable and then catch that variable during pre_get_posts
to set the post_per_page
query_var:
function custom_query_vars_filter($vars) {
$vars[] = 'post_per_page_override';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $query ) {
if( $query->query_vars['post_per_page_override'] == '3') {
$query->set('posts_per_page', '3');
}
}
Then I went even further and make it get the exact amount you want to display in the custom query var:
function custom_query_vars_filter($vars) {
$vars[] = 'post_per_page_override';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $query ) {
if( $query->query_vars['post_per_page_override']) {
$customPPPlimit = $query->query_vars['post_per_page_override'];
$query->set('posts_per_page', $customPPPlimit);
}
}
Worked for me..