Support

Account

Home Forums Add-ons Repeater Field Repeater field value not working with Gravity Forms

Solved

Repeater field value not working with Gravity Forms

  • NOTE: I edited the title and this post because one of the two problems I was having has mysteriously resolved itself…..the field values are now showing in the post edit screen…..HOWEVER I am still having trouble getting those values to dynamically populate a dropdown in a Gravity Form.

    I have ACF Pro ver. 5.2.7 and have added a repeater field to a custom post type.

    When I try to use the data from that field to dynamically populate a Gravity Form’s drop-down, it doesn’t work. I’ve used a code example I found at this page:

    http://support.advancedcustomfields.com/forums/topic/dynamic-population-using-shortcode-acfgravity-forms/

    Modified to fit my use (not using wp_options but wp_postmeta instead) – it does work to modify the dropdown, but the values from the Post’s meta_key don’t populate, so the dropdown just says “Select Room Type”….but no choices are displayed below that.

    Can someone please look at this code and tell me what I’m doing wrong?

    add_filter("gform_pre_render_3", "dynamic_populate");
    
    function dynamic_populate($form) {
        foreach($form['fields'] as &$field){
         
            if(strpos($field['cssClass'], 'dynamicfield') === false)
                continue;
            //Condition : each field which has the class dynamicfield (example). If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form. PS: You can add whichever condition you want. Example : Run only on select fields, run only on select fields AND classname...
            
            global $wpdb;
    	$meta_key = 'room_types_%_room_type';
            $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT *
                FROM {$wpdb->prefix}postmeta
                WHERE meta_key = %s
                ",
    	   $meta_key
            ));
                  
            $choices = array(array('text' => 'Select Room Type', 'value' => ' '));
            //Default choice for dynamic select field. Value is blank, so if it's required it won't validate.
            
            if( $rows ){
                foreach( $rows as $row ) {
                //If there are results, for each result, find the 'repeater row number'
                    preg_match('_([0-9]+)_', $row->meta_key, $matches);
    
                    $name = 'room_types_' . $matches[0] . '_room_type';
                    $value = get_post_meta($name);
                    //Get the subfield value from the row.
    
                    $choices[] = array('text' => $value, 'value' => $value);
                    //Create an option array for each subfield value.
                }  
            }
    
            $field['choices'] = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }
    

    Aside from changing references from the wp_options table to the wp_postmeta table, and using the correct repeater/subfield name, the only other change I made was to add the $meta_key variable and reference it in the SQL query, because using the original example from the link above caused a PHP warning that I was missing an argument (2) in $wpdb->prepare which requires at least two arguments.

    Any suggestions?

  • Oddly, I posted a followup post last night but it’s completely disappeared…..I still really need help with this please!

    I’ve monkeyed around with the code a bit, and now I have it where it displays ALL the repeater field values from the wp_postmeta table, regardless of the Post they are attached to – this is NOT what I want. I want just the values associated with the Post I’m viewing, on which the GF form appears.

    I have a Custom Post Type called “Resorts”, and on each Resort Page I have a repeater field to list the room types available for that resort (e.g. Deluxe, Suite, Oceanview, etc.). Each Resort has different room types.

    On the (front end) Resort Page, I can display those room types just fine along with the rest of the resort info. ALSO on that Page is a (Gravity) Form for the site visitor to choose a room type and dates and get a price quote. The “room types” is a drop-down field that is supposed to auto-populate with the values from the repeater field.

    SO the list of room types to choose from should be isolated to just that Resort – and it’s not, my drop down list is very long and includes ALL of the room types from ALL resorts…..aaargh!

    I am NOT a coder, just trying to piece this together using examples from this website that don’t really fit – the examples are mostly for showing ALL field values, such as the “Diplay all images” example.

    SO here is the code I’m using now – can someone PLEASE tell me how to modify this to ONLY show the repeater field values in the form dropdown from the one POST I’m viewing?

    add_filter("gform_pre_render_3", "dynamic_populate");
    
    function dynamic_populate($form) {
        foreach($form['fields'] as &$field){
         
            if(strpos($field['cssClass'], 'dynamicfield') === false)
                continue;
            //Condition : each field which has the class dynamicfield (example). If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form. 
            
            global $wpdb;	
    	    $rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT *
                FROM wp_postmeta
                WHERE meta_key LIKE %s 
                ",
    			'room_types_%_room_type'
            ));
                  
            $choices = array(array('text' => 'Select Room Type', 'value' => ' '));
            //Default choice for dynamic select field. Value is blank, so if it's required it won't validate.
            
            if( $rows ){
                foreach( $rows as $row ) {
                //If there are results, for each result, find the 'repeater row number'
                    preg_match('_([0-9]+)_', $row->meta_key, $matches);
    
                    $meta_key = 'room_types_' . $matches[0] . '_room_type';
                    $value = get_post_meta( $row->post_id, $meta_key, true );
                    //Get the subfield value from the row.
    
                    $choices[] = array('text' => $value, 'value' => $value);
                    //Create an option array for each subfield value.
                }  
            }
    
            $field->choices = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }
  • OK I finally solved this……..spent hours reading and trying things and finally figured out that I had to get the Post ID first, and make another change from the example I first found, so here is the code that WORKS, I’m posting it in case it might help anyone else trying to use a Repeater Field with values stored in wp_postmeta (not options as the example I found is) to populate a GravityForm dropdown, and needs to limit the values to the individual Post.

    I added comments where needed, you can remove those if you use this.

    // Replaces GravityForms dropdown with list of values from ACF Repeater field
    add_filter("gform_pre_render_3", "dynamic_populate");
    // replace '_3' with the actual ID from your Gravity Form, mine is form ID 3
    
    function dynamic_populate($form) {
        foreach($form['fields'] as &$field){
         
            if(strpos($field['cssClass'], 'dynamicfield') === false)
                continue;
            //This only auto-populates a field which has the class dynamicfield. If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form. 
            
            global $post;
    		$id = $post->ID; 	//  Important - this is what tells the form to only use values from the individual post, not all posts
    		
            global $wpdb;	
    		$rows = $wpdb->get_results($wpdb->prepare( 
                "
                SELECT *
                FROM wp_postmeta
                WHERE post_id = %d AND meta_key LIKE %s 
                ",
    			$id,
    			'room_types_%_room_type'  //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
            ));
                  
            $choices = array(array('text' => 'Select Room Type', 'value' => ' '));
            //Default choice for dynamic select field. Value is blank, so if it's required it won't validate, change the text to what you want it to be, e.g. 'Please Choose:'
            
            if( $rows ){
                foreach( $rows as $row ) {
                //If there are results, for each result, find the 'repeater row number'
                    preg_match('_([0-9]+)_', $row->meta_key, $matches);
    
                    $meta_key = 'room_types_' . $matches[0] . '_room_type';
                    $value = get_post_meta($post->ID,$meta_key,true);
                    //Get the subfield value from the row.
    
                    $choices[] = array('text' => $value, 'value' => $value);
                    //Create an option array for each subfield value.
                }  
            }
    
            $field->choices = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }
    
    
  • Hi TrishaM,

    Sorry about your reply being deleted, I actually replied last night as well and that one’s gone as well. More than likely I clicked something that I wasn’t supposed to, so you can blame me 🙂

    I actually didn’t have the answer and I’m glad you did manage to figure it out. Thank you for posting your solution, I’m sure it will help others in the future.

    ~JH

  • Sorry to resurrect an old thread, but this is exactly what I am looking to do and have modified to my needs, but can not for the life of me figure out how to add another table value into the equation?

    In this example we are looking into the database for:

    ‘room_types_%_room_type’

    in my example I need to also find another:

    ‘room_types_%_room_price’

  • Nevermind just figured it out, I nested the forloops

    Heres my code if it helps someone else, just trying to stop it from returning values that dont relate to the current post:

    function acf_event_cost_options( $form ){
    
        foreach ($form['fields'] as &$field) {
    
            if(strpos($field->cssClass, 'acf_event_cost_options') === false)
                continue;
            //This only auto-populates a field which has the class dynamicfield. If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form.
    
            global $post;
            $id = $post->ID; 	//  Important - this is what tells the form to only use values from the individual post, not all posts
    
            global $wpdb;
            $rows_name = $wpdb->get_results($wpdb->prepare(
                "
                SELECT *
                FROM wp_postmeta
                WHERE post_id = %d AND meta_key LIKE %s 
                ",
                $id,
                'event_cost_structure_%_option_name'  //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
            ));
    
            $rows_price = $wpdb->get_results($wpdb->prepare(
                "
                SELECT *
                FROM wp_postmeta
                WHERE post_id = %d AND meta_key LIKE %s 
                ",
                $id,
                'event_cost_structure_%_option_price'  //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
    
            ));
    
            $choices = array(array('text' => 'Please Choose an Option', 'value' => ' ', 'price' => ' '));
            //Default choice for dynamic select field. Value is blank, so if it's required it won't validate, change the text to what you want it to be, e.g. 'Please Choose:'
    
            if( $rows_name || $rows_price ) {
                foreach ($rows_name as $row_name) {
                    foreach ($rows_price as $row_price) {
                        //If there are results, for each result, find the 'repeater row number'
                        preg_match('_([0-9]+)_', $row_name->meta_key, $matches);
    
                        $meta_key = 'event_cost_structure_' . $matches[0] . '_option_name';
                        $value = get_post_meta($post->ID, $meta_key, true);
                        //Get the subfield value from the row.
    
                        //If there are results, for each result, find the 'repeater row number'
                        preg_match('_([0-9]+)_', $row_price->meta_key, $matches);
    
                        $meta_key_price = 'event_cost_structure_' . $matches[0] . '_option_price';
                        $value_price = get_post_meta($post->ID, $meta_key_price, true);
                        //Get the subfield value from the row.
    
                        $choices[] = array('text' => $value . ' $' . $value_price . '.00', 'value' => $value, 'price' => $value_price);
                        //Create an option array for each subfield value.
                    }
                }
            }
    
            $field->choices = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Repeater field value not working with Gravity Forms’ is closed to new replies.