• Resolved GeraldRezes

    (@geraldrezes)


    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 2 years, 1 month ago by GeraldRezes.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support andrewshu

    (@andrewshu)

    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://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;
    }

    @geraldrezes

    I have added more functions to this code snippet and made it a plugin.

    https://github.com/MissVeronica/um-count-users

    Thread Starter GeraldRezes

    (@geraldrezes)

    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).

    missveronica

    (@missveronicatv)

    @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"]

    Thread Starter GeraldRezes

    (@geraldrezes)

    Nice. Thank you.

    missveronica

    (@missveronicatv)

    @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

    Thread Starter GeraldRezes

    (@geraldrezes)

    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.