Support

Account

Home Forums Front-end Issues Unable to retrieve post objects from PHP Generated fields.

Helping

Unable to retrieve post objects from PHP Generated fields.

  • I’ve successfully created fields on an option page with acf_add_local_field and acf_add_local_field_group but I’m unable to retrieve the value from those fields.

    I’ve got a loop creating multiple post_object fields which except multiple posts. I’d like to retrive the post data and display it within a template. Any thoughts?

    Here’s the code I’m using to create the fields.

    
    if ( defined( 'POLYLANG_VERSION' ) ) {
    	add_filter('acf/fields/post_object/result', 'my_post_object_result', 10, 4);
    	function my_post_object_result( $result, $object, $field, $post ) {
    		
    		global $polylang;
    		
    		$language = $polylang->model->get_post_language($object->ID);
    		
    		$result .= ' (' . strtoupper( $language->slug) . ')';
    
    		return $result;
    	}
    
    	add_filter('acf/fields/post_object/result/name=field_name', 'my_post_object_result', 10, 4);
    }
    
    if ( defined( 'POLYLANG_VERSION' ) && class_exists('acf')  ) {
    
    	// Create a field group for our ACF field to be inserted into. 
    	// It's basically the visual wrapper for the field. 
    	// 
    	function pt_create_acf_local_field_group($group_key, $title) {
    		acf_add_local_field_group(array (
    			'key' => $group_key,
    				'title' => $title,
    				'location' => array (
    					array (
    						array (
    							'param' => 'options_page',
    							'operator' => '==',
    							'value' => 'acf-options',
    						),
    					),
    				),
    			'menu_order' => 0,
    			'position' => 'normal',
    			'style' => 'default',
    			'label_placement' => 'top',
    			'instruction_placement' => 'label',
    			'hide_on_screen' => '',
    			
    		));
    	}
    
    	// Dymanically generates our field(s)
    	function pt_create_acf_local_field($key, $label, $name, $tax, $parent) {
    		acf_add_local_field( array(
    			'key' => $key,
    			'name' => $name,
    			'type' => 'post_object',
    			'parent' => $parent,
    			'post_type' => 'post',
    			'multiple' => 1,
    			'taxonomy' => array('language:'. $tax),
    			'min' => 0,
    			'max' => 3,
    			'instructions' => 'Select items from here to feature in the "feature" area (top section) of the home ' . strtoupper( $tax ) . ' page.',
    			'return_format' => 'object',
    		));
    	}
    
    	if( function_exists('acf_add_local_field') && function_exists( 'acf_add_local_field_group' )  ) {
    
    		$pl_languages = pll_languages_list();
    
    		foreach ($pl_languages as $key => $value) {
    			$group = 'group_' . $key . '_lang_' . $value;
    			$groupTitle = strtoupper( $value ) . ' Featured Posts';
    
    			$key = 'field_lang_' . $value;
    			$label = $value;
    			$name = 'featured_lang_' . $value;
    
    			pt_create_acf_local_field($key, $label, $name, $value, $group);
    			pt_create_acf_local_field_group($group, $groupTitle);
    
    		}
    	}
    }
    

    There is some Polylang stuff going on but I do not suspect that to be created an issue for ACF, although maybe I’m wrong.

    To retrieve the values within the template I’ve tried to retrieve with the follow functions:

    get_field( <VALUE_NAME>, 'option')
    get_field(acf_get_local_field(<VALUE_NAME>, 'option')
    

    acf_get_local_field( <VALUE_NAME>)
    While acf_get_local_field() returned some data, it gave me the entire group, not the actual field I requested.

  • Resolved.

    For some reason it wasn’t working before (life of a dev) but is working now with:

    get_field('FIELD NAME', 'option')

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

The topic ‘Unable to retrieve post objects from PHP Generated fields.’ is closed to new replies.