Support

Account

Home Forums General Issues get_field_objects on programtically generated post

Solved

get_field_objects on programtically generated post

  • I’ve written a function that aims to determine if a field of a given name is defined for a given post. I need this because ACF’s get_field() function returns an empty string in the case of a fieldname that doesn’t exist but also in the case of a field that has been defined, but has a value explicitly set as an empty string.

    It looks like this:

    	public static function field_defined($name, $post_id='') {
    		
    		if(!$post_id){
    			if (!$post_id = get_the_ID()){throw new Exception("$id must be supplied if called outside of loop");}
    		}
    		$post_fields = get_field_objects($post_id);
    		foreach ($post_fields as $post_field) {
    			if ($post_field['name']==$name) {
    				return true;
    			}
    		}
    		return false;
    	}

    This normally works fine but I’m having problems with posts that have been created programatically rather than through the dashboard. Presumably this is because until the post is saved from the dashboard, all the empty values for the fields aren’t yet added to the database.

    Is there any way to programatically create the required entries in posts_meta as are defined in ACF at the same time I create the post?

    Alternatively, is there another way to determine the fields that have been defined for a given post?

  • Actually, ACF returns NULL (which is different than an empty string) if the field is not defined for a post, meaning there is no meta value in the database associated with the post.

    You can get a list of all of the field groups attached to a specific post and then get all the fields in those groups.

    
    // get field groups for the post
    // returns an array of field groups
    $groups = acf_get_field_groups(array('post_id' => $post_id));
    
    // get fields in a group
    // index into the array returned above
    $fields = acf_get_fields($groups[$index]['key']);
    
  • Exactly what I needed. Many thanks.

    Do you know if the results of acf_get_field_groups or acf_get_fields are automatically cached in any way or do they always result in a separate db call?

    Updated version of my code if it’s useful to anyone:

    	public static function field_defined($name, $post_id='') {
    		
    		if(!$post_id){
    			if (!$post_id = get_the_ID()){throw new Exception("$id must be supplied if called outside of loop");}
    		}
    		$groups = acf_get_field_groups(array('post_id' => $post_id));
    		foreach($groups as $group){
    			foreach(acf_get_fields($group['key']) as $field){
    				if($field['name'] == $name){return true;}
    			}
    		}
    		return false;
    	}
  • ACF caches all the groups and fields the first time they are loaded so that additional calls do not result in additional db calls.

  • Perfect. I guess that’s job done then.

    Many thanks for your help.

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

The topic ‘get_field_objects on programtically generated post’ is closed to new replies.