Title: County Counts
Last modified: October 20, 2022

---

# County Counts

 *  Resolved [GeraldRezes](https://wordpress.org/support/users/geraldrezes/)
 * (@geraldrezes)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/)
 * Is there a way to display a count of users from each country when the COUNTRY
   metadata field is used? Example, Brazil: 8, Cuba: 3, United States of America:
   34, etc. Even if I had to filter by Country, I could figure out how to setup 
   the page. TY
    -  This topic was modified 3 years, 7 months ago by [GeraldRezes](https://wordpress.org/support/users/geraldrezes/).

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Support [andrewshu](https://wordpress.org/support/users/andrewshu/)
 * (@andrewshu)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16121422)
 * Hello [@geraldrezes](https://wordpress.org/support/users/geraldrezes/),
 * There is no such function.
    You could try a direct database query.
 * Thank you.
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16124186)
 * [@geraldrezes](https://wordpress.org/support/users/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://wordpress.org/plugins/code-snippets/](https://wordpress.org/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;
       }
       ```
   
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16126599)
 * [@geraldrezes](https://wordpress.org/support/users/geraldrezes/)
 * I have added more functions to this code snippet and made it a plugin.
 * [https://github.com/MissVeronica/um-count-users](https://github.com/MissVeronica/um-count-users)
 *  Thread Starter [GeraldRezes](https://wordpress.org/support/users/geraldrezes/)
 * (@geraldrezes)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16129731)
 * Thank you Miss Veronica,
 * I am testing your plugin at [https://www.asdal.org/testing-page/](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](https://docs.ultimatemember.com/article/1672-displays-country-flag-in-member-directory-and-user-profiles)).
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16130267)
 * [@geraldrezes](https://wordpress.org/support/users/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](https://wordpress.org/support/users/missveronicatv/).
 *  Thread Starter [GeraldRezes](https://wordpress.org/support/users/geraldrezes/)
 * (@geraldrezes)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16130321)
 * Nice. Thank you.
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16132268)
 * [@geraldrezes](https://wordpress.org/support/users/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](https://github.com/MissVeronica/um-count-users)
    -  This reply was modified 3 years, 7 months ago by [missveronica](https://wordpress.org/support/users/missveronicatv/).
 *  Thread Starter [GeraldRezes](https://wordpress.org/support/users/geraldrezes/)
 * (@geraldrezes)
 * [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16133113)
 * Thank you again. The flags are an awesome addition.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘County Counts’ is closed to new replies.

 * ![](https://ps.w.org/ultimate-member/assets/icon-256x256.png?rev=3160947)
 * [Ultimate Member – User Profile, Registration, Login, Member Directory, Content Restriction & Membership Plugin](https://wordpress.org/plugins/ultimate-member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-member/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-member/reviews/)

## Tags

 * [count](https://wordpress.org/support/topic-tag/count/)
 * [country](https://wordpress.org/support/topic-tag/country/)
 * [users](https://wordpress.org/support/topic-tag/users/)

 * 8 replies
 * 3 participants
 * Last reply from: [GeraldRezes](https://wordpress.org/support/users/geraldrezes/)
 * Last activity: [3 years, 7 months ago](https://wordpress.org/support/topic/county-counts/#post-16133113)
 * Status: resolved