Search code examples
wordpresscountcategories

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!


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;