posts_per_page no limit

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

Related posts

Leave a Reply

6 comments

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

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

  3. 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' );
    
        }
    
    }
    
  4. 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.

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

  6. 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;
    }