last day (32 days later) » 

16:15
1
A: Counting posts from a category that has sub categories

VonCYour category structure includes all posts within subcategories of category C: Category C -------------- / | | \ A B X Z / \ / \ / \ / \ ... ... ... But, when counting posts in subcategories, specifically when their names precede th...

Hi VonC Thank you for your idea. I need to have a function that gets the amount of posts in ALL categories, not only the "buggy one". How do I adjust your code?
I was able to combine the two codes. Anyway, your code results in a count of 184 posts. As before. No difference :-(
@JonSnow OK. I have edited the answer to propose an alternative approach.
Ahhhh, with this approach I see the correct amount of posts for the first time! "Category ID 70 has 204 posts." Yeah! But... isn't it of a risk to use the code like this on a public site? Why was the initial approach not working, what's the difference here?
I was able to ask CoPilot for a comparison between my old code and yours. He explained the differences and re-wrote your code by using WP's built-in functions. And it works like a charm!! So, your code did the trick and opened the door, saved my weekend... whatever :-D Thank you very much for your help, I would not have been able to solve it alone (or with CP alone... ;-))
@JonSnow Great, well done! Don't forget to post here as an answer your code.
@JonSnow (I meant to post your own version of your code)
Will do, as soon as I've fixed the latest issues
it's crazy: as long as I am only displaying the count, it's all fine. When I start adding cat name and id to the output, the code somehow changes and the result is wrong again :-(
16:16
@JonSnow: can you post your code in this chat session
@JonSnow: make sure to add @VonCin your message, for me to be notified
16:37
Hi @VonC ! Thank you for your offer to help.... I really appreciate this, as I am really frustrated meanwhile.... t looked as if we had the solution!

The generated code was as follows:

function get_all_category_post_counts() {
$category_args = [
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'post_status' => 'publish',
'pad_counts' => true,
];

$categories = get_categories($category_args);
$category_post_counts = [];

foreach ($categories as $category) {
// Only count posts for parent categories
PS: assigning the posts only to one category is not an option as I need to filter them and can't use tags for this. i also need a category overview for the sub cats
17:22
@JonSnow From your description: posts in categories A and B are also in category E but are not being counted towards E's total when you expect them to. That might be because of how posts are assigned to categories and how these assignments are being counted, especially considering the "E,X" and "E,Y" vs. "A,E" and "B,E" categorization in the admin panel.

So you would need:
- Posts categorized in both parent and subcategories are counted towards the parent's total.
- All categories, including subcategories, are considered, with no posts missed in the count.

  last day (32 days later) »