55

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' => ?
    );
5
  • 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
    Commented Dec 11, 2011 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
    Commented Dec 11, 2011 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
    Commented Dec 11, 2011 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
    Commented Dec 11, 2011 at 18:20
  • @Maugly Thanks for the correction man... just focused on posts_per_page hence copied the typo! ;) Commented Dec 11, 2011 at 19:06

6 Answers 6

112

-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
  • 8
    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
    Commented Aug 11, 2016 at 17:25
  • @toscho Adding your comment as an update to the answer. Commented Nov 23, 2016 at 6:25
  • you save my life!! Commented Oct 11, 2019 at 2:14
  • @DarlanDieterich Glad I could help! :) Commented Oct 14, 2019 at 7:19
33

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

6

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

    }

}
2

Using Ricardo's with some modification:

static $paged;
$post_ids = [];
do {
    $paged++;
    $defaults = [
        'fields' => 'ids', // Only return the ID field to make this query faster.
        'post_type' => 'post',
        'posts_per_page' => 100,
        'no_found_rows' => false, // We need pagination & the count for all posts found.
        'paged' => $paged,
        'update_post_term_cache' => false,
        'update_post_meta_cache' => false,
    ];
    $query = new WP_Query($defaults);
    if ($query->have_posts()) {
        foreach ($query->posts as $id) {
            $post_ids[] = $id;
        }
    }
} while ($query->max_num_pages > $paged);

return $post_ids;

This will dramatically increase the query time by only querying against the ID row and avoiding updating term and meta cache.

1
  • Nice! thanks for sharing. Commented Oct 18, 2019 at 22:25
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

1
  • That's actually a nice addition! Commented Dec 29, 2017 at 0:59
1

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;
}
1
  • 2
    You should add context to explain your code, your idea to solve the question.
    – bueltge
    Commented Feb 22, 2017 at 12:25

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