Home › Forums › General Issues › Relationship field returning ID, how to return the correct values.
Hi guys,
Im having a little trouble returning the correct value with the relationship field.
My end result is to use the relationship field to add selected locations to a drop down in gravity forms. I can populate the drop down and get most of it working.
The issue is when i call the field inside functions.php and print the results to my page all i am is getting is the correct post ID but with get_the_title() its returning the page name of the page itself not the post i am after.
Any help much appreciated.
Been looking for a solution for past few days, with no luck.
add_filter( 'gform_pre_render_4', 'freetrial_studios' );
add_filter( 'gform_pre_validation_4', 'freetrial_studios' );
add_filter( 'gform_pre_submission_filter_4', 'freetrial_studios' );
add_filter( 'gform_admin_pre_render_4', 'freetrial_studios' );
function freetrial_studios( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'studio-list' ) === false ) {
continue;
}
$post_id = $GLOBALS['post']->ID;
$studios = get_field('field_557a974776dd2', $post_id);
if( $studios ) {
$choices = array();
foreach ( $studios as $studio ) {
$studiotitle = get_the_title();
print_r( $studio ); echo '<br>'; print_r( get_the_title() );echo '<br>';print_r( $studiotitle );
$choices[] = array( 'text' => $studio, 'value' => $studio );
$field->choices = $choices;
}
print_r ('<br>'); ( $studio ); echo '<br>'; print_r( get_the_title() );echo '<br>';
$field->placeholder = 'Select a Studio';
$field->choices = $choices;
}
}
return $form;
}
I managed to get the correct wording to display in the drop down. Does this look like best practice for retrieving that info. But its now throwing errors in the wp-admin from this line = $studio_id = $GLOBALS[‘post’]->ID;
add_filter( 'gform_pre_render', 'freetrial_studios' );
add_filter( 'gform_pre_validation', 'freetrial_studios' );
add_filter( 'gform_pre_submission_filter', 'freetrial_studios' );
add_filter( 'gform_admin_pre_render', 'freetrial_studios' );
function freetrial_studios( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'studio-list' ) === false ) {
continue;
}
$studio_id = $GLOBALS['post']->ID;
$studios = get_field('field_557a974776dd2', $studio_id);
if( $studios ) {
$choices = array();
foreach ( $studios as $studio ) {
// Populate the drop down field with values
$choices[] = array( 'text' => $studio->post_title, 'value' => $studio->post_title );
$field->choices = $choices;
}
$field->placeholder = 'Select a Studio';
$field->choices = $choices;
}
}
return $form;
}
Also Any idea on how i could get the value of that selected field, and retrieve a custom field that is related to that post ID eg. a custom email address field that i have set up for each studio.
Thanks
first, sorry for not answering sooner but when you answer your own post it gets removed from the awaiting reply list.
Anyway
I think you were closer on you first attempt. The problem was that you were not supplying the correct post ID for get_the_title.
If you are returning an array of IDs from your relationship field then you should use $studiotitle = get_the_title($studio);
If you’re returning post object from acf then you should use $studiotitle = get_the_title($studio->ID);
Thanks
That makes it much more clearer now. I didnt realise there was a difference between the array and post object setting.
You must be logged in to reply to this topic.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 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.