1

I am very new to this and all I want to do is not limit the number of posts listed on the home page. It currently shows only 20 of about 32, so all the older posts no longer show up. Is there an easy way to make the number of posts shown under recent posts unlimited, or even 99?

1
  • You always want an upper limit, even if it's a silly one like 200, else if you decide in the future you want more than 32 posts, or reuse the code elsewhere it'll come back to haunt you. I've dealt with sites with millions of posts, and those kind of queries show up unexpectedly and bring down servers. You can always put a 'view more' or pagination if it gets out of hand
    – Tom J Nowell
    Commented Aug 6, 2017 at 17:57

1 Answer 1

1

Under settings you can control how many posts there are, you can't set -1 which would be unlimited posts, but you can set a very high number:

enter image description here

But What About just The Homepage?

A lot of people get around this by putting a custom query in their homepage template, but those people are doubling the work WP does and breaking pagination. There are 2 approaches here that are easier and faster to do

Infinite Scrolling

If you have a lot of posts, this will work better, and only load what the user can see. As they scroll down more posts are loaded in so they never reach the bottom ( unless they loaded everything there is to load ).

I would suggest using the infinite scroll provided in Jetpack as a good example, as it works with most themes out of the box

Modifying the main query before it fetches stuff

pre_get_posts is your friend! Don't throw away the main query and create a new one! That's expensive, a waste! Slow!! Instead tell WP you have a few modifications before it goes and grabs the posts, e.g.

add_action( 'pre_get_posts' function( \WP_Query $query ) {
    if ( !$query->is_main_query() || !$query->is_home() ) {
        return; // this isnt the homepage, or the main query, skip!
    }
    $query->set( 'posts_per_page', 100 ); // 100 posts per page on homepage
});

But I want ALL the posts

------Danger Ahead------

If you have 30 posts, and want to show them all, set the posts per page to 200. You'll never reach 200, but on the off chance that you do, your site won't fall over.

You can't predict the future or what the client wants, and code should be resilient. Just because you can tell WP to fetch every single post doesn't mean it's able to. On a large site, it may struggle to load every post into memory, the database may take too long, or it might generate so much HTML the browser never finishes loading and times out. This is why the category and tag UIs use auto-complete instead of listing everything all at once.

So set the number very high, to a number you're sure you'll never reach but your server can still handle.

But if you're feeling especially dangerous, set it to -1. This will fetch everything, even if it's too much for your server to handle.

But I only have 32 posts, it's a small site

Sure, but in a years time your client might import 5000 posts because they need the site to do something it doesn't do right now. You might have a bug that triggers this scenario. You might mess up an import accidentally and now the site times out when you try to load it. You might reuse the code on a different site built for hundreds of posts, who knows? Setting it to -1 is a bad habit that can cause you headaches and trouble in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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