Support

Account

Home Forums Front-end Issues How can you output the Field Label? Reply To: How can you output the Field Label?

  • I know this is an old post, but you can grab the field label from the ‘posts’ table.

      if (!function_exists('getAcfLabelByName')) {
    
        function getAcfLabelByName($fieldName = null) {
    
            if (empty($fieldName)) {
                return 'No field name specified';
            }
    
            // set a default in case there's an exception
    
            $fieldLabel = 'Not Found';
    
            try {
    
                /**
                 * ACF stores the the Labels in the
                 * wp_posts table, where the ACF label is the title
                 * and the excerpt is the field name
                 */
    
                global $wpdb;
    
                $tableName = "{$wpdb->prefix}posts";
    
                $labelReturned = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $tableName 
                WHERE post_type = %s AND post_excerpt = %s", 'acf-field', $fieldName ));
    
                // grab the post_title from the result
    
                $fieldLabel = !empty($labelReturned) ? $labelReturned : 'Not Found';
                
    
            } catch (Exception $e) {
                
                $error = $e->getMessage();
    
                error_log("There was an exception finding the ACF field label by name with the message $error");
    
                
            }
    
            return $fieldLabel;
    
        }
    
      }

    You can then use this like:

    $fieldLabel = getAcfLabelByName('field_name_here');

    For what it’s worth I created a simple plugin to add this function to your site:

    https://wordpress.org/plugins/get-acf-field-label-from-name/