I got these errors:

Warning: Invalid argument supplied for foreach() in C:\OpenServer\domains\TITAN\wp-content\themes\titan\main-page.php on line 185 и т.д. 

The fact is that almost at the very top I have a section in which the latest works from the portfolio are displayed. Isotope is used for sorting and since I didn’t really understand taxonomies. I missed this section. Continued to do other sections. I used the carbon fields plugin to add arbitrary fields to custom entries.

Next, I pull the data from the arbitrary fields I need and draw all this into a layout:

  <div class="row"> <?php $tm_list = carbon_get_the_post_meta('tm_select', 'association'); ?> <?php foreach ($tm_list as $item): ?> <div class="mb-sm-20 wow fadeInUp col-sm-6 col-md-3" onclick="wow fadeInUp"> <div class="team-item"> <div class="team-image"><img src="<?php echo carbon_get_post_meta($item['id'], 'team_img'); ?>" alt="Member Photo"/> <div class="team-detail"> <h5 class="font-alt"><?php echo carbon_get_post_meta($item['id'], 'team_title'); ?></h5> <p class="font-serif"><?php echo carbon_get_post_meta($item['id'], 'team_desc'); ?></p> <div class="team-social"> <?php $st = carbon_get_post_meta($item['id'], 'team_list', 'complex'); ?> <?php foreach ($st as $item2): ?> <a href="<?php echo $item2['team_link']; ?>"><i class="<?php echo $item2['team_icon']; ?>"></i></a> <?php endforeach; ?> </div> </div> </div> <div class="team-descr font-alt"> <div class="team-name"><?php echo carbon_get_post_meta($item['id'], 'team_name'); ?></div> <div class="team-role"><?php echo carbon_get_post_meta($item['id'], 'team_pos'); ?></div> </div> </div> </div> <?php endforeach; ?> </div> 

Then I sorted out the taxonomies and returned to the section where the last 6 works from the portfolio are displayed. To display these records, I myself created a custom_post_type and display them:

 <ul class="works-grid works-grid-gut works-grid-3 works-hover-w" id="works-grid"> <?php $args = [ 'post_type' => 'portfolio_post_type', ]; $works = new WP_Query($args); ?> <?php if($works->have_posts()):?> <?php while ($works->have_posts()):?> <?php $works->the_post();?> <li class="work-item illustration webdesign"> <a href="<?php echo get_permalink(); ?>"> <div class="work-image"><img src="<?php echo get_the_post_thumbnail(); ?>" alt="Portfolio Item"/></div> <div class="work-caption font-alt"> <h3 class="work-title"><?php echo get_the_title();?></h3> <div class="work-descr"><?php echo get_the_excerpt(); ?></div> </div> </a></li> <?php endwhile;?> <?php endif;?> </ul> 

The problem is that after I added the code to display records of recent portfolio work. I got errors:

 Warning: Invalid argument supplied for foreach() in C:\OpenServer\domains\TITAN\wp-content\themes\titan\main-page.php on line 185 и т.д. 

He swears at foreach because after adding the code that displays the latest entries from the portfolio

In a variable:

 $tm_list = carbon_get_the_post_meta('tm_select', 'association'); 

Void is formed !!! those. if you execute var_dump ($ tm_list); then it will output string 0 and if you remove the code that displays the latest work in the portfolio, in the variable again there is O_o data of this type:

 array(4) { [0]=> array(3) { ["id"]=> string(2) "24" ["type"]=> string(4) "post" ["post_type"]=> string(14) "team_post_type" } 

I do not understand where I was wrong. Why, after adding the code that displays the work from the portfolio, I get this error.

    1 answer 1

    The loop should end wp_reset_postdata ().

    Read the article on this topic .

    WordPress always performs the main loop. Inside this loop, certain global variables are defined, including $ post, containing data for the current post.

    Calling carbon_the_post_meta uses this data.

    When you run a custom loop through WP_Query, you override global variables, including $ post by calling $ works-> the_post ().

    Using wp_reset_postdata () will restore the global variables of the main loop and carbon_the_post_meta will return the correct result.

    • Thank you very much. Problem solved. - Slivki show
    • Come again ...) - KAGG Design