Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Created March 16, 2021 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/a42df1ea46c2353fa69a5c5a256a49af to your computer and use it in GitHub Desktop.
Save verygoodplugins/a42df1ea46c2353fa69a5c5a256a49af to your computer and use it in GitHub Desktop.
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