Support

Account

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

  • 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.