Support

Account

Home Forums General Issues Get field key by field name Reply To: Get field key by field name

  • The “real” answer: go to the Field Group admin page, click the “display field key” button in help dropdown, then copy & paste the key into code.

    Maybe closer to the answer you’re looking for:

     function acf_field_key($field_name, $post_id = false){
    	
    	if ( $post_id )
    		return get_field_reference($field_name, $post_id);
    	
    	if( !empty($GLOBALS['acf_register_field_group']) ) {
    		
    		foreach( $GLOBALS['acf_register_field_group'] as $acf ) :
    			
    			foreach($acf['fields'] as $field) :
    				
    				if ( $field_name === $field['name'] )
    					return $field['key'];
    			
    			endforeach;
    			
    		endforeach;
    	}
            return $field_name;
    }
    

    However, this only works for fields registered with PHP via register_field_group().