4

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

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

5
  • 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
    Mar 17, 2019 at 23:49
  • 1
    Oh, should it be in the functions.php? That makes more sense. Thanks
    – Sal
    Mar 18, 2019 at 0:56
  • 1
    Yes, you should place it in functions.php of your active theme.
    – jsmod
    Mar 18, 2019 at 1:22
  • thank you for the code. I used it to display 9 posts per page. But even if the number of posts is displayed correctly, pagination disappeard (for categories with more than 9 posts). I'd like to display 9 posts and leave pagination in place at the bottom of the archive page. How can I archieve that? Thanks! Aug 4 at 6:53
  • @AlessandraDelPuglia don't post comments as solutions to the question. Having only 1 reputation is not an excuse and the system will flag you as a spambot if you continue. If you have a new question, you should ask a new question, you can always link here for context
    – Tom J Nowell
    Aug 4 at 9:54

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.