0

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.

Even in the array that gets printed i cant actually see the actual post name (location name).

Any help much appreciated.

Been looking for a solution for past few days, with no luck.

Heres my code ive come up with.

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;
    }

    // Get the ID of post
    $studio_id = get_the_ID(); 
    $studios = get_field('field_557a974776dd2', $studio_id);


    if( $studios ) {

        $studio_title = get_the_title( $studio_id );    
        $studio_meta = get_post_custom($studio_id);     
        $choices = array();


        $locations = array();
        $p = get_post(); // gets the basic post details
        $f = get_fields(); // gets the ACF details
        $m = array_merge((array)$p, (array)$f); // merges all details into one array
        array_push($locations, $m);


        foreach ( $studios as $studio ) {

            echo '<pre>';
                print_r( get_field('field_557a974776dd2', $studio_id, false) );
                echo '....';
                print_r( get_the_title( $studio_id ) );     
                echo '....';
                print_r( the_title( $studio_id ) );     
                echo '....';
                print_r( $studio_meta );    
                echo '....';    
                print_r( $m );  
                echo '....';                        

            echo '</pre>';

            // Populate the drop down field with values                         
            $choices[] = array( 'text' => $studio, 'value' => $studio );
            $field->choices = $choices;   
        }

        $field->placeholder = 'Select a Studio';
        $field->choices = $choices;        
    }
}
return $form;
}

And heres the output from the printing.

Array
 (
  [0] => 102
  )
  ....Free trial form 2....113Free trial form 2....Array
 (
[_edit_lock] => Array
    (
        [0] => 1437533210:2
    )

[_edit_last] => Array
    (
        [0] => 2
    )

[_wp_page_template] => Array
    (
        [0] => default
    )

[studio_list] => Array
    (
        [0] => a:1:{i:0;s:3:"102";}
    )

[_studio_list] => Array
    (
        [0] => field_557a974776dd2
    )

[email_address] => Array
    (
        [0] => [email protected]
    )

[_email_address] => Array
    (
        [0] => field_55add0bd111c1
    )

 )
 ....Array
(
[ID] => 113
[post_author] => 2
[post_date] => 2015-07-21 14:53:03
[post_date_gmt] => 2015-07-21 04:53:03
[post_content] => Form id 4

[gravityform id="4" title="false" description="false" ajax="true"]
[post_title] => Free trial form 2
[post_excerpt] => 
[post_status] => publish
[comment_status] => open
[ping_status] => closed
[post_password] => 
[post_name] => form-2
[to_ping] => 
[pinged] => 
[post_modified] => 2015-07-22 12:38:31
[post_modified_gmt] => 2015-07-22 02:38:31
[post_content_filtered] => 
[post_parent] => 21
[guid] => http://website.com.au/?page_id=113
[menu_order] => 0
[post_type] => page
[post_mime_type] => 
[comment_count] => 0
[filter] => raw
[studio_list] => Array
    (
        [0] => 102
    )

[email_address] => [email protected]
)
....
2
  • How do you get the reference of the post you're looking for? From $form?
    – vard
    Jul 22, 2015 at 8:17
  • Im not to sure, its part of the gravity forms docs
    – strohy85
    Jul 22, 2015 at 8:24

1 Answer 1

1

I played with the code a few different ways to get the post id and managed to get it working. Answer below

I played with the code for a bit longer and managed to get it working. Answer below

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; this was returning errors in the wp-admin
    $studio_id = get_the_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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.