Home › Forums › Front-end Issues › Relationships with cf "Post object" › Reply To: Relationships with cf "Post object"
I find the solution!
<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
$buildings = get_posts(array(
'post_type' => 'novostroyki',
'meta_query' => array(
array(
'key' => 'ns_zastroyschik', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $buildings ): ?>
<ul>
<?php foreach( $buildings as $building ): ?>
<li>
<?php echo get_the_title( $building->ID ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Thanks!
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.