Created
March 16, 2021 09:14
-
-
Save verygoodplugins/a42df1ea46c2353fa69a5c5a256a49af to your computer and use it in GitHub Desktop.
Show the number of users with a tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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