Home › Forums › General Issues › Show posts that match Post Objects › Reply To: Show posts that match Post Objects
I wrote this, it works by saving variables in a parent loop and using thin in a child loop that checks IDs using an if statement… it is not the cleanest solution, but it does work. If anyone has a better suggestion I am all ears
<?php
$theID = $post->ID;
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'sas_news'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<?php
$pn = get_the_title();
$pid = get_the_ID();
$purl = get_the_permalink();
$pex = get_the_excerpt();
$pimg = get_the_post_thumbnail_url($pid,'full');
?>
<?php $post_objects = get_field('link_to_sas_member'); ?>
<?php if( $post_objects ): ?>
<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<?php if($theID == get_the_ID($post_object->ID) ){ ?>
<div class="post-item">
<div class="ptit">
<a href="<?php echo $purl; ?>"><h3>
<?php echo $pn; ?>
</h3></a>
</div>
<div class="pex">
<?php echo $pex; ?>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.