How to show more posts on an archive page?

I’d like to show more posts on my custom archive page, but can’t seem to figure out the right way to do it. Currently it displays 10, which is the number set in WP Settings and the number I’d like on my homepage. But on archives I’d like more.

Right now the call for posts is super simple:

  <?php if ( have_posts() ) : ?>

                <?php
                // Start the loop.
                while ( have_posts() ) : the_post(); ?>

This works nicely and works with my pagination. Is there a way to modify that to 100 posts? Is this where pre_get_posts would work? Thank you.

1 Answer
1

You can use pre_get_posts action to achieve that:

add_action( 'pre_get_posts', function ($query) {
    if ( ! is_admin() && is_archive() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 100 );
    }
});

In the code above I use is_archive(), but you can use other conditional tags in there…

Leave a Comment