Home › Forums › Front-end Issues › Relationship query – Get url to linked post
Hi!
Has anyone done anything like this:
• Relationship between post type “design” and “item”
• On all of the “design” pages, I’d like to show all “items” from all designs.
• When I click on one of these “items”, id like to go to the “design” linked to that specific item (there will always be just one).
If you know how to do this, could you please let me know? 🙂
Ok, so I’ve used the code in in the tutorial: https://www.advancedcustomfields.com/resources/querying-relationship-fields/#single-location.php and modified it a bit to make it more like I want it:
<?php $lagenheter = get_field('lagenheter'); ?>
<?php if( $lagenheter ): ?>
<h4 class="space">Lägenheter med denna planlösning:</h4>
<div class="img-overlay-wrap halfsized">
<img src="<?php echo get_template_directory_uri(); ?>/img/vaningar_both.svg" />
<svg class="flats" viewBox="0 0 200 200">
<?php foreach( $lagenheter as $lagenhet ): ?>
<?php setup_postdata($lagenhet); ?>
<?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)
*/
$planlosning = get_posts(array(
'post_type' => 'lagenhetstyp',
'meta_query' => array(
array(
'key' => 'lagenheter', // name of custom field
'value' => $lagenhet->ID, // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE',
'posts_per_page' => 1
)
)
));
?>
this doesn’t work. Why is that? Could someone please help me? 🙂
You are missing the quotes from the example
// this
'value' => $lagenhet->ID, // matches exactly "123", not just 123. This prevents a match for "1234"
// should be
'value' => '"'.$lagenhet->ID.'"', // matches exactly "123", not just 123. This prevents a match for "1234"
Hi, thanks for your reply! That doesn’t solve the problem tho, the array $planlosning is still empty, and if I add <?php echo get_permalink( $planlosning->ID ); ?>
to the code, it output links to the current page, which is also of post_type “lagenhetstyp”.
Do you have any other suggestions of what could be wrong in the code? Hope you could help me!
The topic ‘Relationship query – Get url to linked post’ is closed to new replies.
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.