Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show Distinct Category Post Count WordPress



This is my first question to this awesome community, Hope I can get an answer.

I have a situation where I need to show <strong>Distinct post count</strong> for categories.


A post is posted into several categories as these categories are attributes for the post and then also displayed within the post and specific count pages. We are saving region wise data which include Continents and Countries as primary categorization, Apart from that every post has few more categories which are independent and fall in another parent and child categories.

The category hierarchy looks like this ..

-- United States
-- Canada
-- Mexico
-- Germany
-- France
-- Spain
-- Rating 1
-- Rating 2
-- Rating 3
-- Rating 4
-- Issue 1
-- Issue 2
-- Issue 3
-- Issue 4
-- Issue 5

Now a Post is filed under "Germany" with other categories as Europe, Rating 1, Rating 2, Issue 1, Issue 3, Issue 5 selected.

For Europe I need to create a page which lists all categories and shows count but only for posts filed under Europe.

On the home page, I am following code to display the post count for each parents, here <strong>34 = Issues</strong> Parent Category.

'orderby' => 'count',
'order' => 'DESC',
'parent' => '34',
foreach($categories as $category) {
echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.' (' . $category->count .')</a></li> ';

This code works great for me on the home page for each parent category, as there is no segregation and posts are counted within the category. On the Europe page I need to show/count posts in similar manner but posts, which are cross posted in categories along with Europe.

I need to show all Issues and Ratings categories with "Europe's" post count.

How can I add "Europe Category" filter to the code above, or if there is a different workaround.


Answers (2)


Arnav Joy answers:

can you explain your question by giving an example , please

Naveenn comments:

Hi Arnav,

I have already added an example in the question itself, I hope; I was not confusing with the example, I will try again.

I need to show distinct post count for each category on a region specific page.

For Europe, I have created a page and I list all "Issues" and "Rating" categories with their counts, but these counts would be wrong as these for the entire database and not just "Europe"

If the database/site has counts;

All posts = 50

Europe Category = 30

Issues 1 = 10
Issues 2 = 20
Issues 3 = 10
Issues 4 = 25
Issues 5 = 12
Rating 1 = 10
Rating 2 = 12
Rating 3 = 5
Rating 4 = 8
Rating 5 = 4

<strong>But for Europe</strong> the following counts would be true; (example)
<strong>Issues </strong>
Issues 1 = 6
Issues 2 = 12
Issues 3 = 7
Issues 4 = 2
Issues 5 = 18

Rating 1 = 5
Rating 2 = 6
Rating 3 = 15
Rating 4 = 10
Rating 5 = 9

This is what wordpress does on url filter /?cat=25

Posts counts of Category Y in Category X


Abdelhadi Touil answers:

I'm not sure if I understand what you want, but do you mean that you want display all subcategories of a specific category when you are browsing this category?
Example: If you are in "Europe" page (Archive), Then you'll see all post under "Europe" categories, and the page also displays all subcategories of "Europe" category with posts count.
Look at this example:
[[LINK href=""]][[/LINK]]
What I mean is "Subcategories" section, it displays subcategories of current category (Developing in this example).
Is that what you are looking for?

Naveenn comments:


As you can see that "Issues" and "Rating" are not subcategories of Europe, instead they are at a different Parent and Child level.

If it was for subcategories then the code I already have would easily work, that's not the case.

I need to show Subcategories of "Issues" and "Ratings" on a "Europe Page" not archive, but the count of these subcategories should only be for posts also filed in "Europe" category.