Why get_posts are only showing five posts (retrieved by assigning a category to them?

The question:

Here is the link

http://www.brianfunshine.com/voice-work/voice-page/

This is the code:

<?php
/**
 * Template Name: Voice Page (Two Columns)
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */

get_header(); ?>


<?php breadcrumb(); ?>

<?php // this is the main loop ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>

<div class="top-column">

    <div class="post">

        <h2 class="post-title">
            <a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>

        <div class="entry">
            <?php the_content(); ?>
        </div>

        <div class="post-meta-data">
            <?php wp_link_pages('before=<p>'.__('Pages:','options').'&after=</p>'); ?>
        </div>

    </div>

    <?php endwhile; ?>

<?php else: ?>

    <p><?php _e('Sorry, no posts matched your criteria.','options'); ?></p>

<?php endif; ?>

</div><!-- .top-column -->

<div class="left-column">

<?php // retrieve a list of posts with category Voice Audio Demos
$args = array('category_name' => 'Voice Page (Left Column)', 'order' => 'DESC', 'posts_per_page'=>-1);
$customposts = get_posts($args);
foreach($customposts as $post) : setup_postdata($post); ?>

    <div class="post">

        <h2 class="post-title">
            <a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>

        <div class="entry">
            <?php the_content(); ?>
        </div>

    </div>

<?php endforeach; ?>

</div><!-- .left-column -->

<div class="right-column">

<?php // retrieve a list of posts with category Voice Audio Demos
$args = array('category_name' => 'Voice Page (Right Column)', 'orderby' => 'DESC', 'posts_per_page'=>-1);
$customposts = get_posts($args);
foreach($customposts as $post) : setup_postdata($post); ?>

    <div class="post">

        <h2 class="post-title">
            <a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>

        <div class="entry">
            <?php the_content(); ?>
        </div>

    </div>

<?php endforeach; ?>

</div><!-- .left-column -->

<?php get_footer(); ?>

It just retrieves posts with category Voice Page (Left Column) and Voice Page (Right Column). I ahve more than 5 posts in that category bu the page is only showing 5:

enter image description here

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

If you look at the get_posts documentation on Codex, you can see there’s a parameter for number of posts you want to display:

$numberposts
(integer) (optional) Number of posts to return. Set to 0 to use the max number of posts per page. Set to
-1 to remove the limit.

Default: 5

That’s why it’s displaying just 5 posts. You need to add the parameter to your args array:

$args = array(
    'category_name' => 'Voice Page (Right Column)', 
    'orderby' => 'DESC', 
    'posts_per_page'=>-1, 
    'numberposts'=>-1
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment