Support

Account

Home Forums General Issues Get field key by field name

Solved

Get field key by field name

  • Hi Elliot,

    Sorry, I did ask this question before and thought I had the solution. However I’m still searching for the answer.

    Let’s say I only have a field name ($field_name), how can I get the field key only from this variable. So not related to a post id or something else.

    Tried several things but cannot find the right ‘get’ query.

    Thanks!

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

  • Hi,

    I’ve been struggling with this for some time, and this is my solution, I guess it’s not perfect, but is working for me.

    function get_acf_field_keys( $custom_field_slug = '' ) {
    
    		$result = array();
    
    		$meta_key_start = 'field_';
    
    		$acf_args = array(
    			'post_type'		=> 'acf'
    		);
    
    		if ( $custom_field_slug !== '' ) {
    			$acf_args[ 'name' ] = $custom_field_slug;
    		}
    
    		$acf_query = new WP_Query( $acf_args );
    
    		if ( $acf_query->have_posts() ) {
    
    			while ( $acf_query->have_posts() ) {
    
    				$acf_query->the_post();
    
    				$meta_values = get_post_meta( get_the_id() );
    
    				foreach ( $meta_values as $meta_key => $meta_value ) {
    					
    					if ( substr( $meta_key, 0, strlen( $meta_key_start ) ) === $meta_key_start ) {
    
    						$meta_value_array = unserialize( $meta_value[0] );
    
    						$result[ $meta_value_array['name'] ] = $meta_key;
    
    					}
    
    				}
    
    			}
    
    		}
    
    		wp_reset_postdata();
    
    		if ( empty( $result ) ) {
    			$result = false;
    		}
    
    		return $result;
    
    	}

    Basically, I’m using WP_Query to iterate through acf posts (Custom fields) & get the metadata, then filter the metadata starting with ‘field_’ to fill an array using the field name as key and the field key as value, to be used like this:

    if ( $field_keys = get_acf_field_keys( 'acf_sample-custom-field' ) ) {
    
    		if ( isset( $field_keys[ 'sample_field_name' ] ) ) {
    			$sample_field_key = $field_keys[ 'sample_field_name' ];
    		}
    }

    I hope this is helpful for someone.

  • There is a much easier solution given that the field exists on a given post, which you already seems to assume with your current code.

    
    $field = get_field_object('listing_type');
    $field_key = $field['key'];
    

    Done.

  • Thank you very much! I did a slight modification in order to be able to retrieve the name or the field key depending what is given, because I needed the exact opposite. I paste it here for future reference to others. Cheers!

    function acf_get_field_info( $return = 'name', $field_key, $post_id ) {
    		global $wpdb;
    		//choose name 
    		if($return == 'name'){
    			$acf_fields = $wpdb->get_results( $wpdb->prepare( "SELECT ID,post_parent,post_excerpt FROM $wpdb->posts WHERE post_name=%s AND post_type=%s" , $field_key , 'acf-field' ) );
    		}elseif($return == 'key'){
    			$acf_fields = $wpdb->get_results( $wpdb->prepare( "SELECT ID,post_parent,post_name FROM $wpdb->posts WHERE post_excerpt=%s AND post_type=%s" , $field_key , 'acf-field' ) );
    		}
    		// get all fields with that name.
    		switch ( count( $acf_fields ) ) {
    			case 0: // no such field
    				return false;
    			case 1: // just one result.
    				return $acf_fields[0]->post_excerpt;
    		}
    		// result is ambiguous
    		// get IDs of all field groups for this post
    		$field_groups_ids = array();
    		$field_groups = acf_get_field_groups( array(
    			'post_id' => $post_id,
    		) );
    		foreach ( $field_groups as $field_group )
    			$field_groups_ids[] = $field_group['ID'];
    
    		// Check if field is part of one of the field groups
    		// Return the first one.
    		foreach ( $acf_fields as $acf_field ) {
    			if ( in_array($acf_field->post_parent,$field_groups_ids) )
    				return $acf_field->post_name;
    		}
    		return false;
    	}
  • So, the issue I have is that get_field_object() returns an empty value when creating a new post.

    Also, none of these solutions work correctly if you have the same slug in multiple field collections for different post types.

    Really wish ACF has a get_field_info( $post_type, $post_slug );

  • Why can’t you simply use —

    $field = acf_get_field( $field_name );
    $field_key = $field['key'];
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Get field key by field name’ is closed to new replies.