• Resolved myCred

    (@designbymerovingi)


    My users get the option to enter their facebook address (facebook_url) as an extra user_meta and I wanted to know how I could count how many of my users have this user_meta set.

    Any suggestions would be appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could try something like:

    <?php
    global $wpdb;
    $number_of_users_with_facebook_url = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key=%s", 'facebook_url' ) );
    ?>

    Thread Starter myCred

    (@designbymerovingi)

    Thanks for your suggestion!
    It seem to work all though right now it counts everyones meta_value even if the value is empty. How would one adjust this to look for a specific value? If lets say the only options for “facebook_url” is Yes or No and we want to count all Yeses out there.

    Thanks for your help

    Thread Starter myCred

    (@designbymerovingi)

    Some quick mysql online courses and I figured it out.
    Thanks for your help. I put it all into a function like this:

    function count_users_per_meta($meta_key, $meta_value)
    {
    	global $wpdb;
    	$user_meta_query = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key='$meta_key' AND meta_value='$meta_value'" ) );
    	return number_format_i18n($user_meta_query);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Count Users with specific meta’ is closed to new replies.