TransWikia.com

Why is this category page limiting the number of posts?

WordPress Development Asked on October 30, 2021

I am developing my first WordPress theme. I’ve created a custom page for a specific post category, and I would like to display all the posts from this specific category on the page.

The code is largely borrowed from elsewhere, so it’s highly likely that it’s not best practice, but it seems to be working. However, there is one fairly significant problem:

The code I’m using limits the number of posts to the first ten (sorted by alphabetical order). Could anyone tell me how I can change the code I’ve written so that all of the posts in this category will be displayed?

Any help would be very greatly appreciated!

<?php
  $r = new WP_Query(
    apply_filters(
       'widget_posts_args',
         array(
           'post_status'          => 'publish',
           'cat'                   => 5,
           'orderby'               => 'title',
           'order'                 => 'ASC',
         ),
       $instance
    )
  );

  if ( ! $r->have_posts() ) {
    return;
  }
?>
 <ul>
   <?php foreach ( $r->posts as $hof_post ) : ?>
   <?php
     $post_title   = get_the_title( $hof_post->ID );
     $title        = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
     $thumbnail         = get_the_post_thumbnail($hof_post->ID);
     $excerpt           = get_the_excerpt($hof_post->ID);
     $aria_current = '';
   ?>
   <li>
    <a href="<?php the_permalink( $hof_post->ID ); ?>">
      <?php echo $thumbnail; ?>
      <h3><?php echo $post_title ?></h3>
      <p><?php echo $excerpt ?></p>
    </a>
  </li>
  <?php endforeach; ?>
</ul>

One Answer

10 is the default page length which is taken from the Wordpress settings.

It's a little bit hidden but there in the docs for WP Query there's a parameter posts_per_page which can be set to -1 to get all posts, so you just need to add that to the arguments to pass to WP_Query.

(If that doesn't work and you copied that WP_Query code from somewhere, you might need to remove the apply_filters() part and just keep the inner array() and pass that directly to WP_Query)

Answered by mozboz on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP