Support

Account

Home Forums Add-ons Repeater Field Having problems with the_sub_field();

Solved

Having problems with the_sub_field();

  • Please see the code below with notes. I am trying to get the sub field to post out as a checkbox through gravity forms. I have a similar function that works but this fails as the_sub_field(“fitters_name”) returns null. Any idea why this could be, var2console is a function I am using to print console.log to find these results.

    <?php add_filter('gform_pre_render_1', 'populate_checkbox');
    add_filter("gform_pre_submission_filter_1", "populate_checkbox");
    function populate_checkbox($form){
    	global $values;
    	// Stage one
    	// Select all the posts available to the user.Assign the posts to an array named $fields_id.
        foreach($form['fields'] as &$field){
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-posts') === false)
                continue;
    			$posts = get_posts('numberposts=-1&post_status=publish');
    			$user_id_a = get_current_user_id();
            foreach($posts as $post){
    			$key_1_value = get_post_meta( $post->ID, U_S_C_users, true );
    			if ($key_1_value[0] == $user_id_a) {
    				$fields_id[] = $post->ID;
    			}
            }
        }
    	// Stage two
    	// For each field id store each fitters name in the posts array in a new array named $values.
    	foreach($fields_id as $fields){
    		if(get_field('fitters', $fields)):
    			while (has_sub_field('fitters', $fields)):
    						$values[] = the_sub_field('fitters_name');
    						var2console($values);
    			endwhile;	
    			
    		endif;
    	}
    	// Stage three
    	// Select form field id 110 and create checkboxes as documented on the gravity forms site http://www.gravityhelp.com/documentation/page/Gform_pre_render, only using $fitters_name array rather than $post.
        foreach($form['fields'] as &$field){
    		$field_id = 110;
            if ($field['id'] != $field_id)
            continue;
    		foreach ($values as $value) {
    			$choices[] = array('text' => $value, 'value' => $value);
    			$inputs[] = array("label" => $value, "id" => "{$field_id}.{$input_id}");
    			$input_id++;
    		}
            $field['choices'] = $choices;
            $field['inputs'] = $inputs;
        }
        return $form;
    }

    Thanks.

  • Below is a print out of post meta for first post id looped in “Stage 2” noted above.

    ARRAY[26] = array (
      '_edit_last' => 
      array (
        0 => '1',
      ),
      '_edit_lock' => 
      array (
        0 => '1377120757:1',
      ),
      'U_S_C_users' => 
      array (
        0 => 'a:1:{i:0;s:1:"2";}',
      ),
      'U_S_C_message' => 
      array (
        0 => '',
      ),
      'address' => 
      array (
        0 => 'Hotel',
      ),
      '_address' => 
      array (
        0 => 'field_51dd2a41842f7',
      ),
      'city' => 
      array (
        0 => 'Manchester',
      ),
      '_city' => 
      array (
        0 => 'field_51dd2b96938b1',
      ),
      'post_code' => 
      array (
        0 => 'M1 4PS',
      ),
      '_post_code' => 
      array (
        0 => 'field_51dd2ba1938b2',
      ),
      'country' => 
      array (
        0 => 'UK',
      ),
      '_country' => 
      array (
        0 => 'field_51dd2bb6938b3',
      ),
      'fitters_0_fittersname' => 
      array (
        0 => 'Rob',
      ),
      '_fitters_0_fittersname' => 
      array (
        0 => 'field_520e07d86bcd7',
      ),
      'fitters_0_fittersdayrate' => 
      array (
        0 => '90',
      ),
      '_fitters_0_fittersdayrate' => 
      array (
        0 => 'field_520e08306bcd8',
      ),
      'fitters_1_fittersname' => 
      array (
        0 => 'Ben',
      ),
      '_fitters_1_fittersname' => 
      array (
        0 => 'field_520e07d86bcd7',
      ),
      'fitters_1_fittersdayrate' => 
      array (
        0 => '80',
      ),
      '_fitters_1_fittersdayrate' => 
      array (
        0 => 'field_520e08306bcd8',
      ),
      'fitters_2_fittersname' => 
      array (
        0 => 'Phil',
      ),
      '_fitters_2_fittersname' => 
      array (
        0 => 'field_520e07d86bcd7',
      ),
      'fitters_2_fittersdayrate' => 
      array (
        0 => '90',
      ),
      '_fitters_2_fittersdayrate' => 
      array (
        0 => 'field_520e08306bcd8',
      ),
      'fitters' => 
      array (
        0 => '3',
      ),
      '_fitters' => 
      array (
        0 => 'field_520e07c46bcd6',
      ),
    );
  • Got it working by not using the_sub_field.. either way I would like to get to the bottom of this for future use. Many thanks.

    Working script:

    	// Stage two
    	// For each field id store each fitters name in the posts array in a new array named $values.
    	foreach($fields_id as $fields){
    		$rows = get_field('fitters', $fields);
    		if($rows):
    			foreach($rows as $row) {
    						$values[] = $row['fittersname'];
    			}
    		endif;
    	}
  • Hi @luke_r

    Perhaps you needed to use get_sub_field?

  • I have tried that and a few other things and couldn’t find anything obvious checking through your documentation (which is great btw, thanks for the effort). Been playing about with this for a few days now and think that the problem is with storing the value in a variable which has caused the same issue in a seperate script now.

    Other than that small issue with that function though the plugin has been great, saved me a lot of time :).

    Many thanks.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Having problems with the_sub_field();’ is closed to new replies.