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?
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. **
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?
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.
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
But it's not counting posts under children taxonomy. Please see the example URL and screenshots. How to resolve the issue?
Please help me to resolve the issue soon. Thanks.
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.
I am trying to implant the the custom shortcode but not able achieve the desired result. Please help me to accomplish the same.
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.
My issue is resolved now. Thank you!