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
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->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...