I'm not experienced with the WordPress. My goal is to display all the posts on the page.

I was trying to display posts on a page like so:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; else: ?>
  <p><?php _e( 'Sorry, no pages found.' ); ?></p>
<?php endif; ?>

I've faced a problem that there is a limit of 5 posts to be displayed by default. I tried to use custom WP_Query:

  $all_query = new WP_Query(array(
  if ($all_query->have_posts()) : while ($all_query->have_posts()) : $all_query->the_post();

It shows all the posts, but it also shows all the posts even on category archive pages (i.e. posts from another category).

As I understand, I can create archive.php page for categories and authors.

Is there any solution to use loop to show all the posts only of the current category or author?

1 Answer 1


The number of posts displayed in any loop by default is controlled by Settings > Blog pages show at most. To show all posts, you can enter a huge number, but -1 (which is the value to use for the posts_per_page paramerter in WP_Query) does not work here.

It is possible to show all posts on the category and author archives while still showing a limited number of posts within your main blog area. To do this, use the Blog pages to show at most setting to configure the number of posts to show within the main blog, then use the pre_get_posts hook to modify the other archives to suit your preferences. Add the following code to your theme's functions.php file:

 * Modify the query to show all posts on category and author archives.
function wpse238882_pre_get_posts( $query ) {
  if ( ( $query->is_author() || $query->is_category() ) && $query->is_main_query() ) {
    $query->set( 'posts_per_page', -1 );
add_action( 'pre_get_posts', 'wpse238882_pre_get_posts' );

You can still use the author.php and category.php templates to customize the output of your author and category archives, but this is not necessary to simply modify the number of posts displayed, which has been demonstrated above. Check out the template hierarchy Codex entry for more information on customizing templates.

  • I've tried your solution (functions.php file) but it didn't help. In archive.php template, I'm using default loop if (have_posts()) : while (have_posts()) : the_post();. Do I need to change it as well? For instance, if I have 7 posts, only 2 are displayed.
    – Frelseren
    Commented Sep 25, 2016 at 13:45
  • You do not have to change your loop. The code I've posted modifies the query before it is executed and will apply to only the author and category archives. I've tested it with archive.php and without archive.php (which means that index.php was used) and it is working: The main loop shows the number of posts specified under Settings > Blog pages show at most, and category and author archives show all posts. Commented Sep 26, 2016 at 4:20

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.