1

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:

<?php 
  $all_query = new WP_Query(array(
    'post_type'=>'post',
    'post_status'=>'publish',
    'posts_per_page'=>-1,
  ));
  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

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 Sep 25 '16 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. – Dave Romsey Sep 26 '16 at 4:20
0
<?php

//for each category, show all posts
$cat_args = array(
    'orderby'   => 'name',
    'order'     => 'ASC'
);
$categories = get_categories( $cat_args );

foreach( $categories as $category ) {

    $args = array(
        'showposts'         => -1,
        'post_per_page'     => -1,
        'category__in'      => array($category->term_id),
        'caller_get_posts'  =>1
    );
    $posts = get_posts( $args );

    if( $posts ) {
        echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></p>';

        foreach( $posts as $post ) {
            setup_postdata( $post );

            while ( have_posts() ) : the_post(); 

                the_title( '<div>', '</div>' );

            endwhile;hp
        } // foreach($posts
    } // if ($posts
} // foreach($categories
  • 2
    Could you edit your answer and write a paragraph explaining what your code is doing and how it solves the original issue? Code only answers are usually discouraged without some sort of explanation. – Howdy_McGee Aug 6 '18 at 22:00

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.