0

I have updated my Wordpress to the new version 4.0.1 and I got some problems. Before I had an old version, the 3.5.1 and everything was ok with my code. Pardon me for my bad english, I try to explain it.

I have this code:

$user_query = new WP_User_Query(array('ID'));
$users = (array) $user_query;

...

<select name="UsersZone" class="" style="width: 100%" onChange="setZoneUser(this.value);">

<option value="false">--Select A User--</option>

<?php

foreach($users["results"] as $user) {

?>

<option value="<?php echo $user->ID?>"><?php echo $user->display_name;?></option>

<?php

}

?>

</select>

So, with this code I should have a list of my Users. But I receive this error:

Warning: Invalid argument supplied for foreach() in /web/htdocs/www.mywebsite.com/home/... on line xxx

where xxx is the line

    foreach($users["results"] as $user)

I have tried to use var_dump($users) and I got this:

                array(8) {
  ["query_vars"]=>
  array(17) {

...

  }
  ["WP_User_Queryresults"]=>
  array(1) {
    [0]=>
    object(WP_User)#4246 (7) {
      ["data"]=>
      object(stdClass)#4244 (10) {
        ["ID"]=>
        string(1) "1"
        ["user_login"]=>
        string(5) "Admin"
        ["user_pass"]=>
        string(34) "xxx"
        ["user_nicename"]=>
        string(5) "admin"
        ["user_email"]=>
        string(26) "xxx@xxx.com"
        ["user_url"]=>
        string(0) ""
        ["user_registered"]=>
        string(19) "2014-11-26 18:55:12"
        ["user_activation_key"]=>
        string(0) ""
        ["user_status"]=>
        string(1) "0"
        ["display_name"]=>
        string(5) "Admin"
      }
      ["ID"]=>
      int(1)
      ["caps"]=>
      array(1) {
        ["administrator"]=>
        bool(true)
      }
      ["cap_key"]=>
      string(15) "wp_capabilities"
      ["roles"]=>
      array(1) {
        [0]=>
        string(13) "administrator"
      }
      ["allcaps"]=>
      array(63) {

...
  }
  ["filter"]=>
  NULL
    }
  }
  ["WP_User_Querytotal_users"]=>
  string(1) "1"
  ["query_fields"]=>
  string(30) "SQL_CALC_FOUND_ROWS wp_users.*"
  ["query_from"]=>
  string(13) "FROM wp_users"
  ["query_where"]=>
  string(9) "WHERE 1=1"
  ["query_orderby"]=>
  string(23) "ORDER BY user_login ASC"
  ["query_limit"]=>
  NULL
}

So the problem is in the field "WP_User_Queryresults" instead of "results".

How can I solve this problem?

2

You shouldn't be passing in $users["results"] directly to a foreach without checking that $users is an array, and that it has a 'results' key. What if $users is empty, or false, and a WP_Error object?

In this case, it isn't an array, it's a WP_User_Query object. Casting it to an array won't fix that. Instead you should be using it like this:

$user_query = new WP_User_Query( $args );

if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<p>' . $user->display_name . '</p>';
    }
} else {
    echo 'No users found.';
}

Where $user_query->results is an array in the $user_query object

Not like this:

// this will fail!
$user_query = new WP_User_Query( $args );
$user_query = ( array ) $user_query;
foreach ( $user_query["results"] as $user ) {
    echo '<p>' . $user->display_name . '</p>';
}

Further reading

| improve this answer | |
  • Yes, the problem was because I didn't use $user_query->results. Thank you. – Bizio73 Jan 28 '15 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.