0

In my code below the data is extracted correctly for the first foreach, but doesn't return anything for 2nd foreach although selected one present inside the 2nd foreach. I still get the error

Warning: Invalid argument supplied for foreach() for line foreach ($authorlist as $post)...

However the 2nd foreach returns data correctly as soon as I remove the first foreach loop.

Below is my code

<?php

$all= get_posts(array('post_type' => 'books', 'numberposts' => -1,));

foreach ( $all as $post ) :  setup_postdata($post);

    if (!empty($post))
    {
     $booklink = array();

     $booklist = get_field('booklist'); 
               foreach ($booklist as $post) : setup_postdata($post);
                    if (!empty($post))
                        {
                        $booklink[] = strip_tags(get_field('booklink',$post));                                       
                        }
                endforeach;
                wp_reset_postdata();
     echo $booklink[0];  

     $authorname = array();

     $authorlist = get_field('authorlist'); 
               foreach ($authorlist as $post) : setup_postdata($post);
                    if (!empty($post))
                        {
                        $authorname[] = strip_tags(get_field('authorname',$post));                                       
                        }
                endforeach;
                wp_reset_postdata();
echo $authorname[0]; 

    }
endforeach;
?>

I did var_dump($authorlist) and it returns bool(false) as long as foreach ($booklist as $post) loop is present. The moment I remove that loop then the dump shows the data of the authorlist correctly. How to get both the loops working together?

You can see the output here

added unset and change$post for each foreach still not working

<?php

$all= get_posts(array('post_type' => 'books', 'numberposts' => -1,));
echo "VAR_DUMP(all) <br>";
var_dump($all);
echo "<br>";
echo "<br>";
foreach ( $all as $post ): setup_postdata($post);

    if (!empty($post))
    {
     $booklink = array();

     $booklist = get_field('booklist'); 
               foreach ($booklist as $bookpost): setup_postdata($bookpost);
                    if (!empty($bookpost))
                        {
                        $booklink[] = strip_tags(get_field('booklink',$bookpost));                                       
                        }

        endforeach;
        wp_reset_postdata();
        unset($bookpost);


    echo "<br>";
    echo "var dump for booklist";
    echo "<br>";
    var_dump( $booklist);
    echo "<br>";
    echo "<br>";
     echo "booklink is - " . $booklink[0]."<br>";  

     $authorname = array();

     $authorlist = get_field('authorlist'); 
               foreach ($authorlist as $authorpost): setup_postdata($authorpost);

                    if (!empty($authorpost))
                        {
                        $authorname[] = strip_tags(get_field('authorname',$authorpost));                                       
                        }

                endforeach;
        wp_reset_postdata();        
                unset($authorpost);

    echo "<br>";
    echo "var dump for authorlist";
   echo "<br>";
    var_dump( $authorlist);
    echo "<br>";
   echo "<br>"; 
     echo "authorname is - ".$authorname[0]."<br>"; 

    }

endforeach;
wp_reset_postdata();
unset($post);
?>
  • Not sure if this is it, but I don't see any reason for any of your calls to setup_postdata() and wp_reset_postdata(). What happens when you remove them both? – mrwweb Aug 31 '14 at 4:39
  • nothing ...result is same – user3449454 Aug 31 '14 at 4:43
0

As already pointed out by mrwweb and I quote from him

...but I don't see any reason for any of your calls to setup_postdata() and wp_reset_postdata()

You are not trying to setup new posts here, just retrieving existing data from within the post which is already setup

The main reason for your problem is the use of the variable $post in all of your foreach loops. This creates a clash in the foreach loop following the previous one as the $post reference still remains after the foreach loop, thus the unexpected output. Here is what the php manual have to say

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

So, as possible solutions, you can either rename the $post variable to be unique in every foreach loop, or as recommended, unset($post) after each foreach.

Just on a side note, do not use : and endforeach, rather use curly brackets ({}). Curly brackets makes your code easier to debug and to read, and curly brackets are supported by all code editors, where as your method is not

EDIT

As explained, you should remove wp_reset_postdata() and setup_postdata() from you two foreach loops that is inside the main foreach loop. I have also totally rearranged your code. You have to check if your array_expression is not empty before the foreach and wrap that whole foreach loop in that conditional statement.

There is also absolutely no use in checking if $value is empty inside the foreach loop, it will always have a value for as long as there is something inside your array_expression. If your array_expression is empty, you will get the following error regardless if you check if Svalue is not empty

Warning: Invalid argument supplied for foreach()

This is how your code should look like (PLEASE NOTE: this is untested)

<?php

$all= get_posts(array('post_type' => 'books', 'numberposts' => -1,));
echo "VAR_DUMP(all) <br>";
var_dump($all);
echo "<br>";
echo "<br>";


if( !empty( $all ) ) {

foreach ( $all as $post ){

    setup_postdata($post);

     $booklink = array();

     $booklist = get_field('booklist'); 

          if( !empty($booklist) ) {
               foreach ($booklist as $bookpost) { 


                  $booklink[] = strip_tags(get_field('booklink',$bookpost)); 


               }

             unset($bookpost);
          }


    echo "<br>";
    echo "var dump for booklist";
    echo "<br>";
    var_dump( $booklist);
    echo "<br>";
    echo "<br>";
     echo "booklink is - " . $booklink[0]."<br>";  

     $authorname = array();

     $authorlist = get_field('authorlist'); 

           if (!empty($authorlist)) {

               foreach ($authorlist as $authorpost) {


                        $authorname[] = strip_tags(get_field('authorname',$authorpost));                                       

                }

                unset($authorpost);

            }


    echo "<br>";
    echo "var dump for authorlist";
   echo "<br>";
    var_dump( $authorlist);
    echo "<br>";
   echo "<br>"; 
     echo "authorname is - ".$authorname[0]."<br>"; 


}
wp_reset_postdata();
unset($post);
}
?>

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.