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!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.
— Advanced Custom Fields (@wp_acf) February 23, 2023
👉 https://t.co/3UtvQbDwNmhttps://t.co/wfFEVcXVKc
© 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.