Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem counting posts on a category in wordpress

I’m using this code to count posts from a category on my wordpress and it works well but when it arrives at 5 it stops couting. Any help?

  <?php $posts = get_posts('post_type=proyectos&category=13'); 
    $count = count($posts); 
    echo $count; 
    ?>

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In get_posts(), the default number of posts returned is 5. Hence you are getting the count as 5.

If you want the total count of posts in that particular category, you have to pass "-1" to the "number posts" argument inside the function like below.

$posts = get_posts( array(
  'post_type' => 'proyectos',
  'category'  => 13,
  'post_status' => 'publish',
  'numberposts' => -1,
) );
$count = count($posts); 
        echo $count;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading