Home › Forums › General Issues › Getting crazy with Relation field
Hello,
i’ve readed doc on relation field, and it seems to be exactly what i need. Perhaps, i’m not able to get it work on front end. Maybe i’m doing something wrong ?
my process :
i have a travel guide site, and i wish to link custom posts to a city.
so i have custom posts : hotel, camping, etc… and also a custom post called “region” where are all the cities.
In my custom post i added a relation field, linked to the region custom post.
So in each of my hotels, campings, etc… to display “hotel located in CITY”
and in my city pages, i want to display “hotels in CITY”.
i’ve tried both code examples for single-doctor.php & single-location.php i can not have any data displayed. For single doctor, i even have an error message.
# my single location
in my single location, i’m tring to display all the markets of this city.
so i call a function called : be_list_market_in_city()
and my function is :
function be_list_market_in_city() {
echo get_the_ID();
$args = array(
'post_type' => 'marche',
'meta_query' => array(
array(
'key' => 'contact_city', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
echo $title. '<br />';
endwhile;
wp_reset_postdata();
endif;
}
As you can see, i echo get_the_id() to be sure i have the good ID, and on front, i do have ID 1112 display ( 1112 is the right ID for the location )
But i have no results returned.
If i take of the meta_query filter, i do have all the market listed.
What am i doing wrong ?
#my single hotel
in my single hotel, i just made a try with your code in doc, just changing my CPT
<?php
$locations = get_field('contact_city');
?>
<?php if( $locations ): ?>
<ul>
<?php foreach( $locations as $location ): ?>
<li>
<a href="<?php echo get_permalink( $location->ID ); ?>">
<?php echo get_the_title( $location->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
And this return :
Warning: Invalid argument supplied for foreach() in /home/xxx/zz_dns/xxx/wp-content/themes/xxx/template-parts/content-hotelsdetail.php on line 127
i add a capture of my settings (in french) for this field :
Thanks in advance for your precious help in solving this issue.
The topic ‘Getting crazy with Relation field’ 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.