4

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.

5 Answers 5

9

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:

http://core.trac.wordpress.org/ticket/15150

2
  • There's a typo in your parameter for posts_per_page. You have post_per_page instead of posts_per_page. (I tried to edit your post, but I don't have edit permissions). Many thanks!
    – user1462
    Feb 7, 2011 at 21:29
  • Avoid this function, it's very bad for performance, causes lots of issues with pagination, etc. Use pre_get_posts to modify what gets shown on a page, and WP_Query if you want to fetch posts for other reasons. Never use query_posts
    – Tom J Nowell
    Apr 3, 2020 at 15:55
0

Sure, change the query by adding

<?php query_posts('post_type=post&numberposts=-1'); ?>

0

Eileen is right but it's better to use arguments as an array <?php query_posts( array( 'post_type' => 'post', 'numberposts' => -1 ) ); ?>

0

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..

0

If you're using a custom loop, just specify posts_per_page, e.g.:


$query = new \WP_Query( [ 'posts_per_page' => 100 ] );
if ( $query->have_posts() ) {
    ......etc

You could set posts_per_page to -1 for unlimited posts, but this is very bad practice. Instead set however many posts you think your server can serve without falling over so that your site doesn't go down if too many posts are created, 200 is a good upper limit.

Otherwise, use the pre_get_posts filter to modify the parameters passed to queries:

add_action( 'pre_get_posts', function ( $query ) {
    if ( is_admin() ) {
        return;
    }
    $query->set( 'posts_per_page', 100 );
}

Note that this will set all queries to 100 pages, including feeds, REST API, XMLRPC, etc

In addition, some people will recommend query_posts, but this is bad practice and will cause lots of other issues

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.