Wordpress - posts_per_page no limit

-1 is your answer! Look for posts_per_page here.

$args = array(
'post_type'      => 'post',
'cat'            => '22,47,67',
'orderby'        => 'name',
'order'          => 'ASC',
'hide_empty'     => 1,
'depth'          => 1,
'posts_per_page' => -1
);

Important caveat: This can result in a very huge query that can bring the site down. Do this only if you are sure your database can handle it. Not in public themes or plugins.


Or alternatively you can pass WP_Query(which is what query_posts uses) the nopaging argument, which basically does the same thing..

$args = array(
    'nopaging' => true
    // Your other args, etc..
);

It will do exactly the same, but if you have to look back at it later and can't remember what you were doing, i personally feel it will be more clear to you, what it is you were intending with that parameter inside the args array.

As i mentioned however, they'll both actually achieve the same.

Can't hurt to have more than one approach, and it's always nice to share what you know, suffice to say that's the reason for my answer, despite you already having a sufficient one.. ;)


From your child themes functions file:

add_action( 'pre_get_posts', 'wpsites_no_limit_posts' );

function wpsites_no_limit_posts( $query ) {

if( $query->is_main_query() && !is_admin() && is_home() ) {

$query->set( 'posts_per_page', '-1' );
$query->set( 'order', 'ASC' );
$query->set( 'post_type', 'post' );
$query->set( 'cat', '22,47,67' );
$query->set( 'orderby', 'name' );
$query->set( 'order', 'ASC' );
$query->set( 'hide_empty', '1' );
$query->set( 'depth', '1' );

    }

}

Tags:

Query Posts