Support

Account

Home Forums ACF PRO get_fields() breaking after upgrade to ACF PRO on repeater field

Solved

get_fields() breaking after upgrade to ACF PRO on repeater field

  • Here’s the stack trace, looks like the array isn’t getting passed into the array_keys call:

    #1 /Users/mhplummer/Sites/afam-dev/public/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php(522): array_keys('2')
    #2 [internal function]: acf_field_repeater->format_value('2', 165, Array)
    #3 /Users/mhplummer/Sites/afam-dev/public/site/wp-includes/plugin.php(192): call_user_func_array(Array, Array)
    #4 /Users/mhplummer/Sites/afam-dev/public/wp-content/plugins/advanced-custom-fields-pro/api/api-value.php(150): apply_filters('acf/format_valu...', '2', 165, Array)
    #5 /Users/mhplummer/Sites/afam-dev/public/wp-content/plugins/advanced-custom-fields-pro/api/api-template.php(346): acf_format_value('2', 165, Array)
    #6 /Users/mhplummer/Sites/afam-dev/public/wp-content/plugins/advanced-custom-fields-pro/api/api-template.php(470): get_field_object('field_53a878e1a...', 165, true, true)
    #7 /Users/mhplummer/Sites/afam-dev/public/wp-content/plugins/advanced-custom-fields-pro/api/api-template.php(376): get_field_objects(165, true)
    #8 /Users/mhplummer/Sites/afam-dev/app/models/WPObject.php(46): get_fields(165)
    
    

    any ideas?

  • Hi @dinovi

    Thanks for the info. If you enable DEBUG_MODE in your wp-config.php file, do you see any errors which using the get_fields() function?

    You mentioned that it is breaking, but what does that exactly mean?

  • I am experiencing same issue.

    get_fields( ID_OF_POST );

    If there is a repeater field in the fields I am trying to get I get 2 warnings, and the field with that repeater field returns the number of items instead of the array of items like it should.

    Here are the warnings:
    array_keys() expects parameter 1 to be array, string given in /wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 522
    Invalid argument supplied for foreach() in /wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 522

    hopefully this gives more clarification.

  • I found the issue. This happens when the main repeaters sub field has the same field name

  • Hi guys.

    Thanks for all the info in this thread. I’ve done some testing and believe I have found the problem and fixed it!

    Please find bellow a re-written get_field_objects function which will fix the issue. Please copy / paste this over the origional function found in the api/api-template.php file.

    
    /*
    *  get_field_objects()
    *
    *  This function will return an array containing all the custom field objects for a specific post_id.
    *  The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.
    *
    *  @type	function
    *  @since	3.6
    *  @date	29/01/13
    *
    *  @param	$post_id (mixed) the post_id of which the value is saved against
    *  @param	$format_value (boolean) whether or not to format the field value
    *  @param	$load_value (boolean) whether or not to load the field value
    *  @return	(array)	associative array where field name => field
    */
    
    function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) {
    	
    	// global
    	global $wpdb;
    	
    	
    	// filter post_id
    	$post_id = acf_get_valid_post_id( $post_id );
    
    	// vars
    	$meta = array();
    	$fields = array();
    	
    				
    	// get field_names
    	if( is_numeric($post_id) ) {
    		
    		$meta = get_post_meta( $post_id );
    	
    	} elseif( strpos($post_id, 'user_') !== false ) {
    		
    		$user_id = (int) str_replace('user_', '', $post_id);
    		
    		$meta = get_user_meta( $user_id );
    		
    	} elseif( strpos($post_id, 'comment_') !== false ) {
    		
    		$comment_id = (int) str_replace('comment_', '', $post_id);
    		
    		$meta = get_comment_meta( $comment_id );
    		
    	} else {
    		
    		$rows = $wpdb->get_results($wpdb->prepare(
    			"SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
    			$post_id . '_%' ,
    			'_' . $post_id . '_%' 
    		), ARRAY_A);
    		
    		if( !empty($rows) ) {
    			
    			foreach( $rows as $row ) {
    				
    				$meta[ $row['option_name'] ][] = $row['option_value'];
    				
    			}
    			
    		}
    		
    	}
    	
    	
    	// populate vars
    	if( !empty($meta) ) {
    		
    		foreach( $meta as $k => $v ) {
    			
    			// Hopefuly improve efficiency: bail early if $k does start with an '_'
    			if( $k[0] === '_' ) {
    				
    				continue;
    				
    			}
    			
    			
    			// does a field key exist for this value?
    			if( !array_key_exists("_{$k}", $meta) ) {
    				
    				continue;
    				
    			}
    			
    			
    			// get field
    			$field_key = $meta["_{$k}"][0];
    			$field = acf_get_field( $field_key );
    			
    			
    			// bail early if not a parent field
    			if( empty($field) || acf_is_sub_field($field) ) {
    				
    				continue;
    				
    			}
    			
    			
    			// load value
    			if( $load_value ) {
    			
    				$field['value'] = acf_get_value( $post_id, $field );
    				
    				// format value
    				if( $format_value ) {
    					
    					// get value for field
    					$field['value'] = acf_format_value( $field['value'], $post_id, $field );
    					
    				}
    				
    			}
    			
    						
    			// append to $value
    			$fields[ $field['name'] ] = $field;
    		}
    		
     	}
     	
     	 	
    	// no value
    	if( empty($fields) ) {
    	
    		return false;
    	
    	}
    	
    	
    	// return
    	return $fields;
    }
    

    Please let me know how you go after updating the file.

  • Hey Elliot,

    I did a quick test and this seems to be working. Thanks for the great plugin.

  • Hi @jsweazy

    Thanks for testing! Great news

  • I have exactly this problem again. The repeater field is called “fields” – could that be relevant?

    Everything worked fine, but I cannot see what I could have changed that broke this.

    get_fields with this field returns the length of the repeater array rather than its content. Also, this seems to be happening for the field group attached to the options page

  • (even when I use the field_nnn notation, by the way)

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

You must be logged in to reply to this topic.