Skip Navigation

[Resolved] Post counts are not correct

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by deepS 3 years, 6 months ago.

Assisted by: Jamal.

Author
Posts
#1816237

Hi, one small issue left. I am using [wpv-found-count] shortcode to display the number of posts inside the Parent term (including child terms), but it's displaying a wrong count. why?

#1816249

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

I am not really sure to understand the issue, can you elaborate more? Where the shortcode is being used? Where can we see the results on the frontend? What counts are wrong and what values would you expect?

If you would like to allow me temporary access to your site to check this closer, our next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1816339

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Thank you, I confirm that the credentials are working, but I'll still need some details.

Can you please add a screenshot for the wrong counts, so I can easily locate them, and tell me what are the correct values that you would expect?

#1817077
Screenshot_3.jpg
Screenshot_1.jpg

Ok. Let me explain to you with example.
If you go to hidden link (i.e. course archive created with a view suggested by you in https://toolset.com/forums/topic/selective-taxonomy-view/#post-1814669), you will see parent category courses along with the child categories. See screenshot_1. The red squared things are the number of courses (which is wrong).

Now say for example we enter a Parent Taxonomy archive called 'Business & Entrepreneurship Courses' at hidden link. Here you can see the red arrowed figure which is the number of courses under the parent category (this is correct). The number is generated with the shortcode called '[wpv-found-count]'.

Hope I have explained to you things better.

#1818023

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Now, I understand why you are getting (1) as a result. It is expected, as it will be pulled for its parent view(Art and Design) because you have passed a term(One term) to the view.

If you want to get the number of posts for this taxonomy term, use the wpv-taxonomy-post-count shortcode.

[wpv-taxonomy-post-count]

I fixed it on the view and now it works. hidden link
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-taxonomy-post-count

#1818121

But it's not counting posts under children taxonomy. Please see the example URL and screenshots. How to resolve the issue?

#1821931

Please help me to resolve the issue soon. Thanks.

#1822665

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

My apologies for the late reply, but I do not work on Wednesdays and Thursdays.

From what I understand so far, you are looking to get the number of posts on a taxonomy term and its children. For example, the course "Public Speaking", that is assigned to "Professional Development" under "Business & Entrepreneurship" should appear in the count of both terms "Business & Entrepreneurship"(the parent) and "Professional Development"(the child), right?
This is not possible, by default, in Toolset or in WordPress queries.

WordPress only keeps track of the posts that are assigned to a term, it does not count the number of posts of children terms.

So, to get the desired count, for example, 18 posts for the "Business & Entrepreneurship" category, you will have to choose between two solutions:
- Optimal solution: Assign each post to a term and all its parent terms. For example, update the course "Public Speaking" and assign both terms to it ("Professional Development" and "Business & Entrepreneurship").
- Heavy solution: Implement a custom shortcode that will calculate the correct number of posts from the current term and its children. Check this StackOverflow reply, and adapt it to be used in a shortcode https://wordpress.stackexchange.com/a/117092

I hope this answers your question. Let me know if you need further assistance.

#1824267

I am trying to implant the the custom shortcode but not able achieve the desired result. Please help me to accomplish the same.

#1824537

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I drafted the following code and tested it and it seems to work. This shortcode will need two arguments, the taxonomy slug, and the taxonomy term ID. Check this example for categories(slug: category):

[total-tax-posts taxonomy='category' term='[wpv-taxonomy-id]']

Code is:


function jts_total_tax_post_count( $taxonomy, $term_id ) {
    $q = new WP_Query( array(
        'nopaging' => true,
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field' => 'id',
                'terms' => $term_id,
                'include_children' => true,
            ),
        ),
        'fields' => 'ids',
    ) );
    return $q->post_count;
}

function jts_total_tax_posts_func($atts){
  $atts = shortcode_atts(
    array(
      'taxonomy' => 'category',
      'term' => '',
    ),
    $atts
  );
  
  return jts_total_tax_post_count( $atts['taxonomy'], $atts['term'] );

}

add_shortcode('total-tax-posts', 'jts_total_tax_posts_func');

Put this code in your theme's functions.php file or in a snippet in Toolset->Settings->Custom Code.

#1825889

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.