-1

I have a custom built wordpress site and I am recently seeing this error.

Warning: Invalid argument supplied for foreach() in one of the page templates.

It is saying the warning is on this line: foreach ($member_info->instrument as $inst) {

Here is the code:

$directory = array();
foreach ($members as $member ) { // get all the user's data        
    $member_info = get_userdata($member->ID);       
    foreach ($member_info->instrument as $inst) {                
        $directory[$inst][] = array (                                
        'lastname' => $member_info->last_name,                                
        'firstname' => $member_info->first_name,                                
        'address' => $member_info->address_1,                                
        'city' => $member_info->city,                                
        'state' => $member_info->state,                                
        'zip' => $member_info->zip,                                
        'phone' => $member_info->phone,                                
        'email' => $member_info->email  ,
        'instruments' => $member_info->instrument                          
    );

I am researching and seeing that it needs to be in an array and I have tried changing but get formatting errors. I don't know a lot of php so any help is appreciated. The server was recently upgraded to php 5.6 so not sure if the way it is written is deprecated after that update. Thank you!

  • Could you provide us result of var_dump($member_info);? I suppose that $member_info->instrument is not an array and that cause the problem – Sebastian Kaczmarek Nov 2 '16 at 14:12
  • I'm not sure how to do a var_dump? Where would I add this code? Does it go in the header of the page template file? – user2743755 Nov 2 '16 at 18:00
  • It's just for debugging. You can put this code into your functions.php file: do_action('wp_footer', function(){ $directory = array(); foreach ($members as $member ) { // get all the user's data $member_info = get_userdata($member->ID); var_dump($member_info); } }); Note you have to copy all necessary code to get this working. What var_dump does is displaying all the info about variable including type and size. So you can put var_dump($member_info); right in scope of your loop and see what is displayed on the screen – Sebastian Kaczmarek Nov 2 '16 at 19:44
  • here is the codeshare, [link]codeshare.io/bgH9C – user2743755 Nov 3 '16 at 14:41
  • 1
    Here are things you must check: 1. Is $members an array? Make sure it's not NULL. 2. Does $member has ID property? 3. Is $member_info an object? 4. Does $member_info has instrument property? Follow this steps one by one and when everything will be OK then it will work without errors – Sebastian Kaczmarek Nov 3 '16 at 15:31
0

I think there are two possible reason either

$member->ID is null or

this $member_info is not array

Can You give this output to us?

print_r($members)

(I would want to comment but i can't )

  • I am not sure how to do a print_r. Where would I put this to provide you the results? thank you for responding – user2743755 Nov 2 '16 at 18:01
  • just type print_r($members);die(); before foreach loop and give us a result – blackstar Nov 3 '16 at 5:58
  • By the way before diving into theme or plugin development at least please learn basic php fundamentals . – blackstar Nov 3 '16 at 5:59
  • I am sorry, I did not build this custom theme. It came to me seeing if it could be resolved, I apologize for my lack of knowledge on this matter. – user2743755 Nov 3 '16 at 14:22
  • @You don't need to apologize. Just print_r($members) before the code you gave. I mean put print_r($members) before $directory = array(); ........... – blackstar Nov 4 '16 at 6:50

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.