Home › Forums › General Issues › Retrieve specific Post Object's based on custom field › Reply To: Retrieve specific Post Object's based on custom field
Hi @scferg
The issue is 100% using wp_reset_postdata()
.
This function will reset the query back to the origional one set by WP, not the one on line 5! This is very important to understand.
You will need to not use the wp_reset_postdata()
. This is easy, just make use of the get functions like so:
<?php
$post_object = get_field('person_company'); // Person's company Post Object field
if( $post_object ):
$company_name = get_the_title( $post_object->ID ); // Set company name from the person's company
$company_link = get_field('company_link', $post_object->ID); // Set website url for the person's company
?>
<p>
<?php the_field('person_title'); // Custom field from the person post ?>
<a href="<?php echo $company_link; ?>" target="_blank"><?php echo $company_name; ?></a>
</p>
<?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.