I'm using the Advanced Custom Fields plugin to create a metabox with a list of all posts of specific post type, for the user to be able to select related posts for a page. You can see a simple documentation of this functionality here: http://www.advancedcustomfields.com/docs/field-types/post-object/

It's supposed to return a post object. However, I noticed when I try to query it to get any data (title, content, thumbnail etc.) nothing happens. I'm using a foreach loop like in the example (for multiple post chosen) and it returns the following error:

Warning: Invalid argument supplied for foreach()

I thought maybe the option didn't return any post objects, so I've done a var_dump expecting it to spit out null, but instead it showed bool(false).

Now, I'm not a PHP expert by any means, so I'm not sure what that bool(false) means exactly and why would it be what the function returns.

Does anyone have any idea what could be causing the problem? I should add that I get the same result with a very similar function of ACF described here: http://www.advancedcustomfields.com/docs/field-types/repeater/

Thanks in advance!

EDIT: Added code that I'm using:

<div class="insights-features group">

    <?php $featured_articles = get_field('featured_articles'); ?>

    <?php foreach ($featured_articles as $post_object): ?>
        <?php $category = get_the_category($post_object->ID); ?>
        <div class="col2">
            <a href="<?php echo get_permalink($post_object->ID); ?>">
                <?php echo get_the_post_thumbnail($post_object->ID, array(320,302)); ?>
                <p class="descr"><?php echo get_the_title($post_object->ID) ?> by <?php echo get_the_author($post_object->ID) ?> <span>Read In <?php echo $category[0]->cat_name; ?></span></p>
            </a>
        </div>
    <?php endforeach; ?>    

</div><!-- /insights-features -->

<div class="main col4 group">

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

        <div class="post group">
            <div class="post-head">
                <h5 class="category"><?php the_category(', '); ?></h5>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
                <p class="meta"><span class="location">Shanghai</span> <span class="date"><?php the_time('m.d.y, h:iA'); ?></span> by <?php the_author_link(); ?></p>
            </div><!-- /post-head -->
            <?php the_excerpt();?>
        </div><!-- /post -->

    <?php endwhile; ?>

</div><!-- /main -->

This is a snippet from the index.php template – it's supposed to display 3 posts chosen via ACF in insights-features section and then a normal archive loop for the normal flow of posts. As you can tell, I'm using ACF fields outside of the loop.

Also worth mentioning: the custom fields are on a page that is setup to display blog posts in WordPress' settings. Hope that clears things out.

EDIT: Figured it out.

It seems like this didn't work because the page where the custom fields were set up was used as "Posts Page" in WordPress (using index.php file). I copied the code from index.php and created a new page template, set up the page to use this template and removed its selection from "Posts Page" in Reading settings. This seems to have done the trick.

Not sure why it was causing a conflict but it does seem reasonable that it did...

share|improve this question
Are you using the_field/get_field inside or outside The Loop? - please add a summary of your code to the question – brasofilo Apr 26 '12 at 12:15

1 Answer

Did you purchase and activate the repeater field portion of the plugin? That's the only portion that would return an array to use foreach with outside The Loop (that I'm aware of). Either way, the code calling the ACF fields needs to live inside The Loop as @brasofilo said if you're wanting two different chunks like this, you'll find it easier with two loops (with two different queries) on the page, the first to call the 3 posts for the first section, the second to call the normal loop.

If you're stumped, I would start by adding a print_r($featured_articles); after you set that variable on the second line. That will let you know if there's anything in there, and if it's an array that you can actually use foreach with.

share|improve this answer
The Post Object and Relationships modules of ACF also return arrays when there are multiple posts selected: "The API will return either a single post object or an array of post objects. This example shows use of multiple post objects" – I will definitely try making a loops to use the ACF code there. – Justine Apr 26 '12 at 18:41
I think I figured it out, see latest edit. – Justine Apr 26 '12 at 18:57
That would make sense, since the posts page isn't designed to output any content of its own. Glad you got it working again! – SickHippie Apr 26 '12 at 20:34

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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