what is the maximum number of post a wordpress loop can work with?

what is the maximum number of post a wordpress loop can work with?

how many post can i display with ‘posts_per_page’ => -1?

Read More

ive got 1317+ post and I want to sort through them, but I just get a blank page whenever I try and do anything to more than 10,000 pages (‘posts_per_page’ => 10000).

Thank you In advance

Related posts

1 comment

  1. Looks like you run out of your php memory.

    I can think of two options:

    1. Try to add some memory in your php.ini. It won’t actually solve your problem, as your post amount will grow on, and you’ll run into the same issue again.
      But meanwhile it can give you some time to work on real solution 🙂

    2. The real solution, you can use WPDB object to work directly with WP DataBase.
      In that way you can limit amount of posts you’re getting from DB, so you don’t need to store all objects in memory at once.

    In that example I’m displaying category with lots of posts. I’m iterating over DB, getting 100 post each iteration ( using LIMIT), until I get all the posts:

        $sql = "
                SELECT * 
                FROM $wpdb->posts
                LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
                LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
                WHERE post_status = 'publish'
                AND $wpdb->terms.name = '".$cat_name."'
                AND $wpdb->term_taxonomy.taxonomy = 'category'
                AND post_type = 'post' 
                AND post_title not regexp '".$excluded_posts."'
                ORDER BY post_date DESC LIMIT 100 OFFSET $offset
                ";
    
            $posts_in_category = $wpdb->get_results($sql, OBJECT);
            ?>
    
        <!-- Some HTML here -->
        <?php while ($posts_in_category): ?>
                <?php if ($posts_in_category): ?>
                    <?php global $post; ?>
                    <?php foreach ($posts_in_category as $post): ?>
                        <?php setup_postdata($post); ?>
                        <!-- Here you can pot your post template -->
                    <?php endforeach; ?>
                <?php else: ?>
                    <p><?php _e('No posts matched your criteria.', 'wp-print'); ?></p>
            <?php endif; ?>
    
    <?php $offset += 100; 
        $sql = "
            SELECT * 
            FROM $wpdb->posts
            LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
            LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
            LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
            WHERE post_status = 'publish'
            AND $wpdb->terms.name = '".$cat_name."'
            AND $wpdb->term_taxonomy.taxonomy = 'category'
            AND post_type = 'post' 
            AND post_title not regexp '".$excluded_posts."'
            ORDER BY post_date DESC LIMIT 100 OFFSET $offset
            ";
    
        $posts_in_category = $wpdb->get_results($sql, OBJECT);
    ?>
    <?php endwhile;?>
    

    Good Luck 🙂

Comments are closed.