Home › Forums › Front-end Issues › relationship – impossible to get permalink
Hello Guys,
thanks for reading me.
i’m getting troubles on my site.
i use relationship fields to link cities to custom posts.
so i wish to display the city & a link to the city page on different areas.
if use code related to docs, something really easy :
$event_cities = get_field('contact_city',$post->ID);
foreach($event_cities as $event_city) {
echo '<a href="'. get_permalink( $event_city->ID ) .'">'. get_the_title( $event_city->ID ) .'</a>';
}
but for a reason that i can not identify :
– title is OK
– permalink is wrong.
on a single.php page, the permalink display the link of the loaded page.
on a page like homepage where i link future events & want to display the event city with a link to it, it display link of the event.
i’m getting totaly crazy ! I readed an make hundred of tests !
Any help will be apreciate !
Hello,
after many investigation, i found what is making this trouble !
its a custom code i used to add a .html extension to all my pages & customs posts.
but i can not figure what in this code is creating this trouble :s
my function
function custom_post_permalink ($post_link) {
global $post;
if($post) {
$type = get_post_type($post->ID);
$post_type_data = get_post_type_object( $type );
$post_type_slug = $post_type_data->rewrite['slug'];
$post_type_slug_translated = apply_filters( 'wpml_get_translated_slug', $post_type_slug, $type);
$translated_home_url = apply_filters( 'wpml_home_url', home_url());
$be_current_lang = apply_filters( 'wpml_current_language', NULL );
if($be_current_lang=='fr'){
return $translated_home_url . $post_type_slug_translated . '/' . $post->post_name . '.html';
} else {
return $translated_home_url .'/'. $post_type_slug_translated . '/' . $post->post_name . '.html';
}
}
}
add_filter('post_type_link', 'custom_post_permalink');
Thanks in advance for your help
In order for this to work with your function you are going to need to use setup_postdata(). See the basic loop section of the documentation https://www.advancedcustomfields.com/resources/relationship/
Hello,
i already use setup_postdata()
my complete function :
global $wpdb,$post;
$results = $wpdb->get_results( "SELECT * FROM wp_posts, <code>wp_mec_dates</code> AS mecd, wp_icl_translations WHERE wp_posts.ID = mecd.post_id and post_status='publish' AND wp_icl_translations.language_code='$lang' AND dstart>'$startday' and wp_posts.ID = wp_icl_translations.element_id ORDER BY dstart LIMIT 0,5" );
if (count($results)> 0){
echo '<h3 class="widget-title"><span><i class="far fa-calendar-alt"></i> Les prochains événements</span></h3>';
foreach ($results as $post) {
setup_postdata($post);
$event_permalink = get_the_permalink();
$event_date = $post->dstart;
$new_event_date = date("d/m", strtotime($event_date));
$event_edate = $post->dend;
$new_event_edate = date("d/m", strtotime($event_edate));
$event_title = get_the_title();
$image_url = get_the_post_thumbnail_url('','liste-etablissements');
$event_cities = get_field('contact_city');
echo '<div class="events-sidebar"><div class="events-img">'."\n";
echo ' <a href="'. $event_permalink .'"><img src="'. $image_url .'" alt="" class="image"></a>'."\n";
echo ' </div>'."\n";
echo ' <div class="events-detail">'."\n";
echo ' <h3 class="presta-title"> <a title="" href="'.$event_permalink.'">'. substr($event_title,0,55) .'</a></h3>'."\n";
echo ' <div class="presta-decouvrir">'; if($new_event_edate != $new_event_date) { echo 'Du'; } else { echo 'Le'; } echo ' <b>' .$new_event_date; if($new_event_edate != $new_event_date) { echo '</b> au <b>'. $new_event_edate; } echo '</b><br />';
if($event_cities) { $cities = 0; $max_cities = 1; foreach($event_cities as $event_city) { $cities++; if($cities > $max_cities) { break; } echo 'à <a href="'. get_permalink( $event_city->ID ) .'" title="">'. get_the_title( $event_city->ID ) .'</a>'; } }
echo ' </div>'."\n";
echo ' </div></div><div style="clear:both;"></div>'."\n";
//echo $new_event_date . ' - <a href="'.$event_permalink.'" title="'.$event_title.'">' . substr($event_title,0,38) .'</a><br />';
}
}
wp_reset_postdata();
every data works, exept the relationship permalink.
if i deactivate my custom function in previous post, everything works well. But i don’t see in my function what is creating this trouble.
Also, i repeat, the get_the_title() works good.
with exactly the same settings. ( settings is relationship ID )
so this confirm the post data are good.
if i add this code before header, no whories at all. Its only when this hook take action on it.
I would do this
// before getting link
remove_filter('post_type_link', 'custom_post_permalink');
// get the link
// after getting the link
add_filter('post_type_link', 'custom_post_permalink');
Hello, thanks for your reply !
your answear seems good but not totaly fix the issue (not tested), because i would be oblige to use it anytime i wish to display those links.
Just got an answear on Stackexchange, my problem was that i was defining global $post in my function.
if you want more explaination, here is the thread
https://wordpress.stackexchange.com/a/339859/139936
Now I understand your custom query is also in a function, that wasn’t clear enough for me. Glad you got a solution.
english is not my 1st language 😀 sorry 😛
indeed, thanks for taking time to look at this issue.
The topic ‘relationship – impossible to get permalink’ 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.