• 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: https://pastebin.com/0jnpcU5B

    Any advice is much appreciated.
    Thanks!
    -M

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    (@ninjapiratespy)

    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.