3

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.

3

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

  • Excellent. And where is this correctly placed? After have_posts(), after the while statement, or at the end of the entire section where I'd normally add an action? Thank you! – Sal Cangeloso Mar 17 at 23:49
  • Oh, should it be in the functions.php? That makes more sense. Thanks – Sal Cangeloso Mar 18 at 0:56
  • Yes, you should place it in functions.php of your active theme. – jsmod Mar 18 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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