• Hallo WordPress Community,

    I have a Woocommerce shop site with multiple authors. In the sidebar I want to display a list of woocommerce product categories by author (based on posts which an author made), accompanied by a product post counter. I use following code by 2famousTV, which works fine to get the categories by author and a counter. I modified it so it only shows parent categories.

    <?php
    
    global $post, $wpdb;
    
    // This will get us a list of the categories that our Author has published in
    $author = get_query_var('author');
    $categories = $wpdb->get_results("
    
    SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
    FROM $wpdb->posts as posts
    LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    WHERE posts.post_status = 'publish' AND
    posts.post_author = '$author' AND
    tax.taxonomy = 'product_cat'
    ORDER BY terms.name ASC
    ");
    
    // This loop picks up categories
    foreach($categories as $category) : 
    
    $catid = $category->ID;
    
    // Now, inside the loop, we need to count how many posts that the Author has published.
    $counter = "SELECT COUNT(*)
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID =     $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.term_id = $catid
    AND $wpdb->term_taxonomy.taxonomy = 'product_cat'
    
    // modification  
    
    AND tax.parent = '0'
    
    // modification end
    
    AND $wpdb->posts.post_status = 'publish'
    AND post_author = '$author'
    ";
    
    $user_count = $wpdb->get_var($counter);
    
    echo '<div class="archive_author">' . $category->name . '<br/><span    class="subcounter">' . $user_count . ' posts</span></div>';
    
    endforeach; 
    
    ?>

    Source: Link to original post by 2famous-tv

    My question:

    With this code the parent category counter only counts items within the parent category but not the items within its child categories. How can I make the parent category counter include the posts of the child category without actually assigning the posts to the parent category?

    Category________________Counter___________Posts

    Parent-Category___________0__________________0

    -Child-Category A_________2__________________2

    -Child-Category B_________3__________________3

    should be:

    Category_______________Counter___________Posts

    Parent-Category___________5__________________0

    -Child-Category A_________2__________________2

    -Child-Category B_________3__________________3

    I tried it with several WordPress inherid options like “wp_list_categories”, “get_terms” and “get_the_term” but did not bring results. It seems the only way to go through Mysql. I am not firm enough to make that happen. Any help would be appriciated!

  • The topic ‘Count posts of child category in parent category’ is closed to new replies.