0

On a WP page we are counting and displaying the amount of posts for all categories. Meaning, all sub category posts should be counted as posts for the main category.

Let's say, the name of the main category is "E" and the sub categories are "A", "B", "X" and "Z".

My function seems to be wrong. It's counting the posts from "X" and "Z" to category "E", but it's not counting the posts from "A" and "B".

What can be the reason for this? Is it code related or can there be another mistake? I have double checked that slugs are unique and that the hierarchy from main and sub categories is correct.

This is my current code:

$categories = get_categories([
     'hide_empty' => 0,
     'parent'     => 0,
]);
                                
foreach ($categories as $category) {
    $subcategories = get_categories([
        'hide_empty' => 0,
         'parent'     => $category->term_id,
    ]);
                                
    $total_count = $category->count; // Initialize with the main category count
                                
    foreach ($subcategories as $subcategory) {
        $total_count += $subcategory->count; // Add subcategory counts
    }
                                
    echo '<input type="checkbox" data-select="sectionFilter" id="section_' . $category->term_id . '" value="' . $category->term_id . '">';
    echo '<label for="section_' . $category->term_id . '">';
}

EDIT: Why is this problem?

Because they start with a letter that comes before C in the alphabet? I've been looking for the error in the code for so long now, but can't find anything that helps. Now I have also made A times TestA and from B a TestB. And then it counts correctly.

How can I solve this without renaming the subcategories??

1 Answer 1

0

Every time you step through the outermost foreach() loop, your $total_count gets reset.

Try this:

$categories = get_categories([
     'hide_empty' => 0,
     'parent'     => 0,
]);

$total_count = 0;
                                
foreach ($categories as $category) {
    $subcategories = get_categories([
        'hide_empty' => 0,
         'parent'     => $category->term_id,
    ]);
                                
    $total_count += $category->count; // Adds the main category count
                                
    foreach ($subcategories as $subcategory) {
        $total_count += $subcategory->count; // Add subcategory counts
    }
                                
    echo '<input type="checkbox" data-select="sectionFilter" id="section_' . $category->term_id . '" value="' . $category->term_id . '">';
    echo '<label for="section_' . $category->term_id . '">';
}
2
  • Thanks for your reply. Will test as soon as I am back in the Office on Monday. What makes me wonder is that the existing function does work (does count correctly) once I rename sub cat A and B to (for example) TestA and TestB.
    – JonSnow
    Commented Mar 16 at 15:33
  • Hi Pat Your code brings the same result than mine, no difference. It still counts 184 posts instead of 204. 2 sub categories are not being count as long as the keep their original name
    – JonSnow
    Commented Mar 18 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.