@geraldrezes
You can try this code snippet and use the shortcode:
[count_users_custom meta_key="country"]
or any other meta_key.
Install the code snippet into your active theme’s functions.php file
or use the “Code Snippets” plugin.
https://www.ads-software.com/plugins/code-snippets/
add_shortcode( 'count_users_custom', 'count_users_custom_shortcode' );
function count_users_custom_shortcode( $atts ) {
global $wpdb;
if( empty( $atts['meta_key'] )) return 'No meta_key';
$meta_key = sanitize_text_field( $atts['meta_key'] );
$users = $wpdb->get_col( "
SELECT meta_value
FROM {$wpdb->usermeta}
INNER JOIN {$wpdb->users} ON user_id = ID
WHERE meta_key = '{$meta_key}'" );
$options = array( 'empty' => 0 );
foreach ( $users as $value ) {
if( !empty( $value ) ) {
if( isset( $options[$value] )) $options[$value]++;
else $options[$value] = 1;
} else $options['empty']++;
}
ob_start();
echo '<h4>Total number of users found: ' . count( $users ) . '</h4>';
foreach( $options as $key => $count ) {
echo '<div style="display: table-row;">';
echo '<div style="display: table-cell;">' . $key . '</div>';
echo '<div style="display: table-cell;text-align:right;">' . $count . '</div>';
echo '</div>';
}
$output = ob_get_contents();
ob_end_clean();
return $output;
}