I want to return ALL posts with query_posts. I tried setting posts_per_page to a really high number, but query_posts freaks out and doesn't return any posts. What is the correct way to query posts without a limit?

$args = array(
    'post_type'      => 'post',
    'cat'            => '22,47,67',
    'orderby'        => 'name',
    'order'          => 'ASC',
    'hide_empty'     => 1,
    'depth'          => 1,
    'posts_per_page' => ?
    );
  • 6
    I've been Googling for awhile and searching the WP codex, but I just can't find a direct answer to this simple question. I believe my question is worded clearly with example code, and what my attempt was (setting a high value for the argument). I'm not a WP expert, so thats why I came here to ask the question. Even providing answers to questions that seem trivial to you are helpful in growing these Stack Exchange communities. I personally love seeing a Stack Overflow link in my search results, as opposed to a link to a crappy forum. – Banjer Dec 11 '11 at 17:50
  • Also, thanks for the answer. You should post it as an answer and not a comment, so I can accept it. – Banjer Dec 11 '11 at 17:51
  • I've got your point and I do appreciate your effort in writing the question. I also agree that non-expert questions can prove themselves valuable in this community. On the other hand too many of such questions can discourage some experts from getting involved here. It's all about some kind of balance I guess. Anyway I'm a big up-voter so I'm looking forward to your next questions :) Have a great time here on WPSE. – Michal Mau Dec 11 '11 at 18:15
  • PS: you also want to replace type for post_type (or remove this line altogether). I will edit both the Rutwick's answer and your question to prevent somebody from copy-pasting this little mistake. – Michal Mau Dec 11 '11 at 18:20
  • @Maugly Thanks for the correction man... just focused on posts_per_page hence copied the typo! ;) – Rutwick Gangurde Dec 11 '11 at 19:06
up vote 72 down vote accepted

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

  • 4
    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. – fuxia Aug 11 '16 at 17:25
  • @toscho Adding your comment as an update to the answer. – Rutwick Gangurde Nov 23 '16 at 6:25

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' );

    }

}

The right answer for your issue is 'posts_per_page' => -1 because -1 will return unlimited posts per page As the others users answer.

I just want to add an add-on to this Q/A,

If you want to get the number of posts per page from the reading setting on WordPress Administration Panel you have to call the get_option() function and pass posts_per_page as a string to it.

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

I hope this answer will help someone as it help me. Happy Coding Stackexchange Users

Or..

function getAll($arg, $posts_per_page = 50)
{
    $data = array();
    $page = 1;

    do{
        $arg['paged'] = $page;
        $arg['posts_per_page'] = $posts_per_page;

        $query = new WP_Query($arg);

        foreach ($query->posts as $post){
            $data[] = $post;
        }

        $page++;

    }while(count($query->posts) === $posts_per_page);

    return $data;
}
  • 2
    You should add context to explain your code, your idea to solve the question. – bueltge Feb 22 '17 at 12:25

protected by Community Feb 22 '17 at 12:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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