• Resolved Seth Carstens

    (@sethcarstens)


    I tried to use the following, which from WP documentation should show all the fields of all “subscribers”. However, I’m not sure your plugin is properly extending the user profiles now… because this code does NOT give me any of your custom meta data… Can you please indicate if this is true? If so, can you tell me how to query the custom meta data of all users by filtering them with a query? For example, our list has NFL teams, I want “all users with NFL team Arizona Cardinals”, or better yet, “all users with NFL team != None”.

    //get all users as objects that are subscribers
    	$args       = array('role' => 'subscriber', 'fields' => 'all_with_meta');
    	$user_query = new WP_User_Query($args);
    	if (!empty($user_query->results)) {
    		echo json_encode($user_query->results);
    	} else {
    		echo 'No users found.';
    	}

    https://www.ads-software.com/plugins/user-meta-manager/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jason Lau

    (@jason-lau)

    You could do something like this –

    global $wpdb;
    $data = $wpdb->get_results("SELECT a.*, b.* FROM $wpdb->usermeta a, $wpdb->users b WHERE a.user_id = b.ID AND meta_key = 'NFL_team' AND meta_value != 'None'");
    foreach($data as $d):
       $user = $d->user_nicename;
       echo $user . '<br />';
    endforeach;

    Plugin Author Jason Lau

    (@jason-lau)

    In the future, I will be extending the PHP API for this plugin to include functions like this.

    Thread Starter Seth Carstens

    (@sethcarstens)

    So the answer is, you don’t properly extend the user profiles so that the inherent WP_User_Query works?

    Plugin Author Jason Lau

    (@jason-lau)

    When coded properly, the WP_User_Query class works just fine. I was simply offering an alternative method.

    Thread Starter Seth Carstens

    (@sethcarstens)

    Indeed, then I’m wondering why the example from the Codex does not return any values for User Profile Custom Meta Data, as it claims it should.

    https://codex.www.ads-software.com/Class_Reference/WP_User_Query#Custom_Field_Parameters

    https://codex.www.ads-software.com/Class_Reference/WP_User_Query#Return_Fields_Parameter

    I was intending on filtering results using built-in query objects, just like you do with query posts, but I cannot get it to return any of the values created by UMM plugin. Any ideas?

    Plugin Author Jason Lau

    (@jason-lau)

    $args = array(
       'meta_key' => 'NFL_team',
       'meta_value' => 'None',
       'meta_compare' => '!=',
       'role' => 'Subscriber',
       'fields' => 'all_with_meta'
    );
    $user_query = new WP_User_Query( $args );
    if ( ! empty( $user_query->results ) ) {
    	foreach ( $user_query->results as $user ) {
               //print_r($user);
    	   echo '<p>' . $user->NFL_team. '</p>';
    	}
    } else {
    	echo 'No users found.';
    }
    Plugin Author Jason Lau

    (@jason-lau)

    Thread Starter Seth Carstens

    (@sethcarstens)

    Wow, I must apologize. I was trying to use var_export to get a list of the data compiled in this object, but apparently this is part of that “magic constants” or whatever they call it, and it cannot be printed this way… Your suggestion works just fine, but now I don’t have a way to iterate through the meta fields. Any idea how to dump all the objects values?If not, no sweat, I appreciate you taking support this far.

    Plugin Author Jason Lau

    (@jason-lau)

    The example I posted previously joins the wp_users and wp_usermeta tables on the user id fields, and the resulting data is very easy to traverse. I recommend using this method.

    global $wpdb;
    $data = $wpdb->get_results("SELECT a.*, b.* FROM $wpdb->usermeta a, $wpdb->users b WHERE a.user_id = b.ID AND meta_key = 'NFL_team' AND meta_value != 'None'");
    foreach($data as $d):
       $user = $d->user_nicename;
       echo $user . '<br />';
    endforeach;

    I’m fairly certain you will use fewer resources this way also.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘User Query Doesn't work?’ is closed to new replies.