Home » Php » php – Warning: Invalid argument supplied for foreach() on filter

php – Warning: Invalid argument supplied for foreach() on filter

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a column where I’m pulling in objects from Recipe and getting the names back, but some posts don’t have ‘required’ authors yet and I’m getting a debug error back – Is it possible to set a ‘No authors available’ echo if there are no array/objects being returned?

Here is the code:

add_action('manage_post_posts_custom_column', function($column, $post_id) {
    switch ($column) {
        case 'authors':

            $recipe = Recipe::init($post_id);

            if (is_array($recipe) || is_object($recipe))
            {
                foreach ($recipe->get_authors() as $profile) {
                    $arr[] = '<a href="' . $profile->get_url() . '">' . $profile->get_name() . '</a>';
                }
                echo implode($arr, ', ');
                break;
            } else {
                echo 'No authors';
            }
    }
}, 10, 3);

Here in the screenshot below, you can see my first post has authors, but the second post doesn’t have them set yet.

enter image description here

var_dump(is_object($recipe)) returns true for the first post.

Could someone tell me what I might be doing wrong?

public function set_authors($authors)
{
    $profiles = [];

    foreach ($authors as $author) {
        $profiles[] = Profile::init($author);
    }

    $this->authors = $profiles;
}

public function get_authors()
{
    return $this->authors;
}
How to&Answers:

Here you go! You also had a problem in implode, you were passing the array then the glue, but it had to be the other way around.

add_action('manage_post_posts_custom_column', function ($column, $post_id) {
    switch ($column) {
        case 'authors':

            $recipe = Recipe::init($post_id);

            if (is_array($recipe) || is_object($recipe)) {
                $authors = $recipe->get_authors();

                if (isset($authors) && !empty($authors)) {
                    $arr = array();
                    foreach ($authors as $profile) {
                        $arr[] = '<a href="' . $profile->get_url() . '">' . $profile->get_name() . '</a>';
                    }
                    echo implode(', ', $arr);
                    break;
                }
                echo "No Authors";
                break;
            } else {
                echo 'No authors';
            }
    }
}, 10, 3);

Update, change set_authors to:

public function set_authors($authors)
{
    $profiles = [];
    if(!empty($authors)){
        foreach ($authors as $author) {
            $profiles[] = Profile::init($author);
        }
    }
    $this->authors = $profiles;
}

Answer:

You must Check on
$recipe->get_authors()

And not on $recipe

I can understand that $recipe is an object but $recipe->get_authors() is the array that you use on foreach.

If so you have to check if $recipe->get_authors() is an array before your foreach