Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to echo the number of posts for the current month from all categories but it isnt working.

<?php
$month = date('m');
$year = date('Y');
$countposts = get_posts('post_type=post&year=' . $year . '&monthnum=' . $month . '&cat=3');
echo 'New posts '  . count($countposts);
?>

category 3 is a parent category and has many sub categories. No matter what I do, it returns the value 5. I have published 30 posts for this month to test it but it doesnt count them. :(

What Im I doing wrong?

Update I have tried the following query and returns the same outpout

    <?php
wp_reset_postdata();
$current_year = date('Y');
$current_month = date('m');
$args = array(
    'cat'      => 3,
    'year'     => $current_year,
    'monthnum' => $current_month,
    'post_status'     => 'publish',
    'post_type'       => 'post'
);
$number_posts = get_posts( $args );
echo 'New posts '  . count($number_posts);
?>
share|improve this question
Try using this : $month = date('n'); – Vinod Dalvi Feb 27 at 13:04
Hi thanks, same output, I even tried F, M, n, m – Jose David Garcia Llanos Feb 27 at 13:11

1 Answer

This is because the paramenter posts_per_page of the get_posts() function defaults to 5. Set it to -1 in your $args.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.