0

I have this PHP code that produces a warning:

Warning: Invalid argument supplied for foreach() php wordpress

Here is the code:

<?php   
    $post_status1  = 'publish'; 
    $post_type1 = 'page';
    $featucat = "about";
    $featucount = "1";

    $my_query = new WP_Query('post_status='. $post_status1 .'&post_type='. $post_type1.'');  
    if ($my_query->have_posts()){
        while ($my_query->have_posts()) : $my_query->the_post();

            $front_values = get_post_custom_values('Homepage_Blog_01p', get_the_ID());
            foreach ( $front_values as $front_key => $result_value ) {
                if($result_value == 'about') {
?>

                    <div class="thewidgets">
                        <?php
                        $description_values = get_post_custom_values('Description_Field', get_the_ID());
                        foreach ( $description_values as $description_key => $description_value ) {
                            echo $description_value;

                        }
                        ?>
                        <a href="<?php the_permalink(); ?>" title="Read the whole post" class="rm">Read More</a>
                    </div>
    <?php } } endwhile; } ?>

Here is the full error:

Warning: Invalid argument supplied for foreach() in 
D:\PROGRAM FILES\wamp\www\westchester\wp-content\themes\
computerrepair\footer.php on line 22"

What am I doing wrong?

3 Answers 3

1

$front_values is not an array if you're getting that. Check its contents, and if it's legitimately not an array at times (for example, if get_post_custom_values returns null when there aren't any results), account for it by wrapping the foreach in an if(is_array($front_values)) { conditional.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the answer, base on what you said, I added if(is_array($front_values)) { that wraps foreach ( $front_values as $front_key => $result_value ) { if($result_value == 'homepage1') { and the error is gone but yet no content is showing :(
If nothing is showing, get_post_custom_values('Homepage_Blog_01p', get_the_ID()) isn't returning anything. Check its output by print_r($front_values).
yes, I tried print_r($front_values) it and nothing has been outputted so I could conclude that it isnt really returning anything, so what should I do to make it show? any idea?
thank you! i already figured it out, thanks for the guidance :)
0

How to reproduce this PHP warning:

Put this in a.php:

<?php
$skipper = "abcd";
foreach ($skipper as $item){       //warning happens on this line.
    print "ok";
}

?>

Prints:

eric@dev ~ $ php a.php
PHP Warning:  Invalid argument supplied for foreach() in 
/var/www/sandbox/eric/code/php/run06/a.php on line 3
PHP Stack trace:

The warning means exactly what it says. You passed a parameter into the foreach structure which could not be evaluated in the foreach. Before the foreach loop, ensure that the first parameter is a structure that the foreach can handle.

Comments

0
<?php wp_head(); ?>

Add this code in the head tag. I think it will solve your warning - Warning: Invalid argument supplied for foreach() in C:\wamp64\www\development\wp-includes\script-loader.php on line 2652

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.