Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show the number of users with a tag
<?php
//
// Tag Count shortcode, use like [wpf_tag_count tag="Tag Name"]
//
function wpf_tag_count( $atts ) {
$tag = wpf_get_tag_id( $atts['tag'] );
$result = get_transient( 'wpf_tag_count_' . sanitize_key( $tag ) );
if ( false !== $result ) {
return $result;
}
$args = array(
'fields' => 'ids',
'meta_key' => wp_fusion()->crm->slug . '_tags',
'meta_value' => '"' . $tag . '"',
'meta_compare' => 'LIKE',
);
$users = get_users( $args );
// Cache the result for one day
set_transient( 'wpf_tag_count_' . sanitize_key( $tag ), count( $users ), DAY_IN_SECONDS );
return count( $users );
}
add_shortcode( 'wpf_tag_count', 'wpf_tag_count' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment