0

The past couple of hours I am trying to find out what is the problem with this...

<?php $related_posts = get_field('related_posts'); ?>
<?php if ($related_posts) : ?>
    <div class="related-projects_items-list-container">
        <div class="comments-area-title">
            <h4 class="hN"><?php _e('<em>Related</em> Articles', wpgrade::textdomain()); ?></h4>
        </div>
        <ul class="related-projects_items-list grid">
        <?php
        foreach ($related_posts as $post_object) {
            $the_post_ID = $post_object->ID;
            $the_permalink = get_permalink( $post_object->ID );
            $the_title = get_the_title( $post_object->ID );
            $the_thumbnail = get_the_post_thumbnail( $post_object->ID, array(200,150) );
            $the_post_author = $post_object->post_author;
            ?>
            <li class="related-projects_item grid__item one-half lap-and-up-one-quarter" style="margin-bottom: 20px;">
                 <article class="article article--billboard-small">
                    <a href="<?php echo $the_permalink; ?>">
                        <div class="article__thumb">
                            <?php if (has_post_thumbnail( $the_post_ID )) echo $the_thumbnail; ?>
                        </div>
                        <div class="article__content">
                            <h2 class="article__title article--billboard-small__title">
                                <span class="hN"><?php echo $the_title; ?></span>
                            </h2>
                            <span class="article__description">
                                <?php $author_url = get_author_posts_url( $the_post_author ); ?>
                                <?php $author_name = get_the_author_meta( 'display_name', $the_post_author ); ?>
                                <?php echo $author_name; ?> - <?php echo get_post_time('d.m.Y', false, $the_post_ID, false); ?>
                            </span>
                            <span class="small-link"><?php _e('Read More', wpgrade::textdomain()); ?><em>+</em></span>
                        </div> 
                    </a>
                </article>
            </li>
        <?php } ?>
        </ul>
    </div>
<?php endif; ?>

I get this...

Warning: Invalid argument supplied for foreach() in /wp-includes/post-thumbnail-template.php on line 64

The problem is this line...

$the_thumbnail = get_the_post_thumbnail( $post_object->ID, array(200,150) );

I can't understand why! Can someone please help?

I am using WordPress v.3.9.1

2
  • Have you made any modifications to WordPress Core? Anything that modifies or changes/removes the main query?
    – Tom J Nowell
    Commented Jun 28, 2014 at 9:28
  • 1
    I have not made modifications to WordPress Core.
    – kanenas
    Commented Jun 28, 2014 at 19:12

3 Answers 3

0

This Trac ticket here describes your problem and the cause:

https://core.trac.wordpress.org/ticket/26321

I got a warning/error related to update_post_thumbnail_cache() function:

Warning: Invalid argument supplied for foreach() in ./wp-includes/post-thumbnail-template.php on line 64 The $wp_query->posts array is missing, but we have the single $wp_query->post.

foreach ( $wp_query->posts as $post ) {
  if ( $id = get_post_thumbnail_id( $post->ID ) )
      $thumb_ids[] = $id;
}

Ticket #26321 describes that when a custom WP_Query loop on a page tries to grab a post thumbnail, Core tries to cache the thumbnails of the posts in the current query.

The problem happens because when doing so it loops over the posts in the query, but it never checks if the array of posts is empty or not. If it is, then you get the warning.

For the moment I would wait for a resolution in WP Core. The warning isn't breaking anything in your codes logic ( though a warning is still, undesirable ).

You can help though, log in to trac, download the patches and help test them. The more testing and help you can provide, the faster the bug can be fixed and the sooner it can get into WordPress Core.

If you manage to fix the bug yourself and satisfy all the unit tests, you might get props! Your name would be on the about/contributors page in every WordPress install running that release

2
  • Thanks for the answer! I thought too that maybe this was my problem. So I applied this patch core.trac.wordpress.org/attachment/ticket/26321/26321.patch and the warning disappeared.
    – kanenas
    Commented Jun 29, 2014 at 19:23
  • Make sure that you make it clear you had this issue and the patch corrected the problem on the trac ticket so that it can be included in WP Core sooner
    – Tom J Nowell
    Commented Jun 29, 2014 at 20:34
0

Add a if statement, that you have a thumbnail, before you get the thumbnail.

if ( has_post_thumbnail( $post_object->ID ) )
     $the_thumbnail = get_the_post_thumbnail( $post_object->ID, array(200,150) );
3
  • Do you mean this <?php if (has_post_thumbnail( $the_post_ID )) echo $the_thumbnail; ?>. I have it already.
    – kanenas
    Commented Jun 27, 2014 at 15:33
  • No, I mean that you add the if statement before you get the pot thumbnail. You write in the question, then the warning is from this line.
    – bueltge
    Commented Jun 27, 2014 at 19:50
  • I have tried that, still I get the warning!
    – kanenas
    Commented Jun 28, 2014 at 19:14
-1

I don't know why you think the post thumbnails are to blame, but the warning mentioned a foreach. If we remove the html we see this:

$related_posts = get_field('related_posts');
if ( $related_posts ) {
    foreach ( $related_posts as $post_object ) {

Clearly this is the cause of your warning.

Since you're returning a value for a field that has multiple options, this would be a better check:

$related_posts = get_field('related_posts');
if ( !empty( $related_posts ) ) {
    foreach ( $related_posts as $post_object ) {

This will now catch:

  • empty arrays
  • null
  • empty strings
  • false values
4
  • Thank you for your answer but I still get the warning!
    – kanenas
    Commented Jun 27, 2014 at 15:49
  • 1
    Is it possible to have this core.trac.wordpress.org/ticket/26321 problem!?
    – kanenas
    Commented Jun 27, 2014 at 15:57
  • @Tom J Nowell it certainly isn't. If the warning was generated by trying to loop over $related_posts if it wasn't an array, it wouldn't specify the error to originate in wp-includes/post-thumbnail-template.php (Warning: Invalid argument supplied for foreach() in /wp-includes/post-thumbnail-template.php on line 64).
    – engelen
    Commented Jun 27, 2014 at 19:04
  • Indeed I misread that part
    – Tom J Nowell
    Commented Jun 29, 2014 at 8:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.