Hello @geraldrezes,
There is no such function.
You could try a direct database query.
Thank you.
@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://ww.wp.xz.cn/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;
}
@geraldrezes
I have added more functions to this code snippet and made it a plugin.
https://github.com/MissVeronica/um-count-users
Thank you Miss Veronica,
I am testing your plugin at https://www.asdal.org/testing-page/. It faithfully displays a list of countries with counts.
If you are willing to consider some enhancements…
It would be nice to sort by the country name.
It would be cool to use the UM country flag plugin and display the flag before the country’s name. (https://docs.ultimatemember.com/article/1672-displays-country-flag-in-member-directory-and-user-profiles).
@geraldrezes
Thanks for your feedback.
Sorting of country names should work if you use this shortcode parameter:
[um_count_users_custom meta_key="country" sort="meta_value"]
-
This reply was modified 3 years, 7 months ago by
missveronica.
@geraldrezes
Version 2.0.0 of the plugin is now availale with country flags.
Use this shortcode where countryflags now is an option for all meta_key names like country and billing_country.
[um_count_users_custom meta_key="country" sort="meta_value" countryflags="true"]
https://github.com/MissVeronica/um-count-users
-
This reply was modified 3 years, 7 months ago by
missveronica.
Thank you again. The flags are an awesome addition.