Home › Forums › Add-ons › Flexible Content Field › Get relationship from custom post
Hi,
I have a custom post that are actors, actresses…and then a normal post that are news related to that actors, actresses…I create a relationship field in the news but i don’t know how from the custom post actors (actors, actresses…) i can get the related news to them.
This is my code. Do a query for getting de related field of the news and then compare to de ID of current actor and show results…It’s complicated and don’t work as expected…
$terms1 = $post->ID;
$query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'nociticias_relacionadas',
'compare' => 'EXISTS'
)
)
)
);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
$relacion = get_field('nociticias_relacionadas', $relacion->ID);
if(!empty($relacion)):
foreach( $relacion as $p ):
$terms2 = $p->ID;
endforeach;
endif;
if ($terms2 == $terms1){
$image = get_the_post_thumbnail( $query->ID, 'noticias' );?>
<li class="noticiasr" style="display: inline;">
<div class="parafade" style="float:left; max-width: 212px; text-align:center; font-size:12px; color:#aaaaaa; margin-right: 6px; line-height: 18px;">
<a href="<?php echo get_permalink(); ?>"> <?php echo $image; ?> </a>
<?php echo get_the_title(); ?>
</div>
</li>
<?php
}
What will be a best and working solution? I don’t get it…
Thanks and regards,
Hi @raulbofill
I need some clarification on what you want to achieve.
So you have regular posts in which you have a relationship field where the user can connect actors/actresses to posts. And now you want to display the posts related to these actors/actresses when on a single actor/actress page?
Hi,
Yes it’s exactly what i want and i figure out how to solve.
I mess up with the code and i see it’s quite simple. I attach the final code in custom posts.
Thanks and regards,
$related_articles = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'nociticias_relacionadas', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if( $related_articles ):
foreach( $related_articles as $article ):
$imag = get_the_post_thumbnail( $article->ID, 'noticias' );?>
<li class="noticiasr" style="display: inline;">
<div class="parafade" style="float:left; max-width: 212px; text-align:center; font-size:12px; color:#aaaaaa; margin-right: 6px; line-height: 18px;">
<div class="fondo"><a href="<?php echo get_permalink($article->ID); ?>"> <?php echo $imag; ?> </a></div>
<?php echo get_the_title($article->ID); ?>
</div>
</li>
endforeach;
endif;
Hi @raulbofill
Alright great that you figured it out! It’s quite simple when you how how to do it and I believe there are tutorials for these things under documentation as well.
Good luck with your project!
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.