Hi there, I have a reverse relationship setup with a return format of Post Objects. I have followed your instructions (thank you) and most data comes through perfectly but can’t get the_content or the_exceprt from the relationship. I have mucked about trying get_the_content but cannot get anything to appear 🙁 Am I missing something? Renee
<?php $items = get_posts(array( 'post_type' => 'post', 'meta_key' => 'nursery', 'orderby' => 'date', 'order' => 'ASC', 'meta_query' => array( array( 'key' => 'nursery', 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' )) )); ?> <?php if( $items ): ?> <?php foreach( $items as $item ): ?> <div class="news"> <?php $photo = wp_get_attachment_url( get_post_thumbnail_id($item->ID), 'news' ); $content = the_content( $item->ID ); // have tried get_the_content ?> <a href="<?php echo get_permalink($item->ID); ?>" <div class="nursery-news clearfix"> <img src="<?php echo $photo ?>" alt="<?php echo get_the_title( $item->ID ); ?>" width="180" /> // gets thumbnail okay <h3 style="clear:none"><?php echo get_the_title( $item->ID ); ?></h3> <?php echo get_the_date( $item->ID ); ?> // gets date okay <?php $author = get_the_author( $item->ID ); ?> <?php echo $author; ?> // gets author okay <p><?php echo $content; ?></p> // no content appearing - have tried excerpt also </div></a> <?php endforeach; ?> <?php endif; ?>
Hi renee, kylu –
the reason this won’t work is that the_content and get_the_content are only meant to be used within ‘The Loop’ and therefore do not accept a post id parameter.
my personal solution to this would be using WP_Query directly (rather than get_posts) to create a secondary loop, but within the context of your code above you can probably get around this through the use of setup_postdata as in the example here:
and remember to call wp_reset_postdata after the fact.
EDIT: as an afterthought… you should be able to access all post data directly from your $items array the same way you are accessing the id. the only problem is that this bypasses all content filters so you would have to apply them manually.
$content = apply_filters( 'the_content', $item->post_content );
Been away from the desk. Thanks for responding. Of course, not in the loop – makes total sense and now have it working. Setup_postdata was the answer.
Code for anyone looking for it:
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => 1, 'meta_query' => array( array( 'key' => 'field', // name of custom field 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ) ) ); $lastposts = get_posts( $args ); foreach ( $lastposts as $post ) : setup_postdata( $post ); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p><?php the_content(); ?> <?php endforeach; wp_reset_postdata(); ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.