Title: Category post count in loop
Last modified: August 20, 2016

---

# Category post count in loop

 *  [NinjaPirateSpy](https://wordpress.org/support/users/ninjapiratespy/)
 * (@ninjapiratespy)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/category-post-count-in-loop/)
 * Hi,
 * I am trying to get the number of posts assigned to each category within a foreach
   statement and turn the get_the_category Multi-dimensional array that is returned,
   into a simple count array. I have tried array_count and array_filter with no 
   luck. Below is the link to the relevant code. I am using the advanced custom 
   fields plugin if the get_field function looks unfamiliar but it shouldn’t distract
   from the general principle. The overall goal is to count the amount of times 
   a category is used for featured posts and then use that number to determine whether
   or not to load more of that category.
 * code: [http://pastebin.com/0jnpcU5B](http://pastebin.com/0jnpcU5B)
 * Any advice is much appreciated.
    Thanks! -M

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Chandan Kumar](https://wordpress.org/support/users/chandanonline4u/)
 * (@chandanonline4u)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/category-post-count-in-loop/#post-3010751)
 * To get the posts count of particular category, use the following code:
 *     ```
       $thisCat = get_category($catId,false);
       $total_posts = $thisCat->count;
       echo $total_posts;
       ```
   
 *  Thread Starter [NinjaPirateSpy](https://wordpress.org/support/users/ninjapiratespy/)
 * (@ninjapiratespy)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/category-post-count-in-loop/#post-3010897)
 * Hi Chandan,
 * Thanks for the quick reply. If I am not mistaken that would get me the total 
   count of posts from that category. I am trying to get only the number of times
   each category appears in a specific query. In this instance posts selected from
   the Advanced Custom Fields relationships field.
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Category post count in loop’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [NinjaPirateSpy](https://wordpress.org/support/users/ninjapiratespy/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/category-post-count-in-loop/#post-3010897)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
