Skip Navigation

[Resolved] Taxonomy count snippet

This support ticket is created 3 years, 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by matthewL-7 3 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2164395

Hi

I am using a code snippet I found on Toolset's forum to count taxonomy for a post.

The code is below this used to work but since updates either by Toolset or WordPress itself it no longer works:

function count_post_terms( $atts ) {
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
            'taxonomy' => '',
        ),
        $atts
    );
    $terms = wp_get_post_terms( $atts['post_id'], $atts['taxonomy']);
    return count($terms);
}
add_shortcode( 'count_post_terms', 'count_post_terms' );

Here is where I am using the shortcode:

[wpv-conditional if="( [count_post_terms post_id='[wpv-post-id]' taxonomy='country'] gte 4 ) AND ( [count_post_terms post_id='[wpv-post-id]' taxonomy='country'] lt 40 )"]
<div class="tooltip">[count_post_terms post_id='[wpv-post-id]' taxonomy='country'] Countries<span class="tooltiptext">[wpv-post-taxonomy type="country" format="slug"]</span></div>
[/wpv-conditional] 

[wpv-conditional if="( [count_post_terms post_id='[wpv-post-id]' taxonomy='country'] gte 40 )"]
<div class="tooltip">[count_post_terms post_id='[wpv-post-id]' taxonomy='country'] Countries<span class="tooltiptext">See Offer Page</span></div>
[/wpv-conditional]

When I debugged the results for when I used the above in a Toolset conditional it always give the result of 0.

Any ideas how we can fix?

#2164461

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please make sure that the shortcode "count_post_terms " is registered at:
=> Toolset => Settings => Front-end Content tab => Third-party shortcode arguments

More info:
=> https://toolset.com/documentation/legacy-features/views-plugin/shortcodes-within-shortcodes/#third-party-shortcode-arguments

If the shortcode is already registered then I will require to debug and you will require to share information where you added the shortcode on what page/post, the link of problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2170805

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the shortcode you added as given under:

function count_post_terms( $atts ) {
  global $post;
    // Attributes
    $atts = shortcode_atts(
        array(
            'post_id' => '',
            'taxonomy' => '',
        ),
        $atts
    );
  
 
    $terms = wp_get_object_terms( $post->ID, $atts['taxonomy']);
    return count($terms);
}
add_shortcode( 'count_post_terms', 'count_post_terms' );

Can you please confirm now you can see the results as expected:
=> hidden link

#2171877

Awesome thank you, all sorted.