Getting “Invalid argument supplied for foreach()” Warning…

  • # July 11, 2013 at 2:22 am

    I am on the last step in a tutorial that I followed [here](http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-4-using-the-data/ “http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-4-using-the-data/”) , called “Using the data”.

    I tried using//

    < ?php $album_repeatable = unserialize($post_meta_data[0]);
    echo '[ul class="album_repeatable"]';
    foreach ($album_repeatable as $string) {
    echo '[li]'.$string.'[/li]';
    }
    echo '[/ul]';
    ?>

    But I get the Warning as stated in Discussion Title//

    “_Warning: Invalid argument supplied for foreach()_ “

    What exactly is the issue here, in terms I could understand? lol
    Please help me fix this?…

    # July 11, 2013 at 3:46 am

    **SOLUTION**//
    Used normal method to get field data instead of “_unserialize_”

    Switched this line:

    $album_repeatable = unserialize($post_meta_data[0]);

    to this line:

    $album_repeatable = get_post_meta($post->ID, 'album_repeatable', true);

    Now my repeatable field Data is being displayed.

    __
    # July 11, 2013 at 11:30 am

    This reply has been reported for inappropriate content.

    $album_repeatable = unserialize($post_meta_data[0]);

    Is that part of the array serialized (I don’t see any reason it would be)?

    *****
    ######EDIT_

    in any case, the underlying issue is that you’re passing something to foreach that is not foreach-able. When unserialize() fails, for example, it returns false, which foreach cannot iterate over. It’s also possible that $post_meta_data[0] _was serialized, and unserialize() was successful, but the serialized value didn’t represent an array or iterable object.

    # July 11, 2013 at 4:05 pm

    @traq I appreciate you trying to explain, just wish I understood it lol!
    But yeah…thank you :)

    __
    # July 11, 2013 at 4:10 pm

    This reply has been reported for inappropriate content.

    No problem. Basically, I was trying to figure out why you chose to try unserialize in the first place – I thought it was a little unlikely that your data was serialized in the first place …?

    # July 14, 2013 at 3:55 am

    @traq I was just following the tutorial, that’s all. hehe :)

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed