acf get field key is commonly used in WordPress. AFC stands for Advanced custom fields plugin. This plugin help add custom fields to posts, pages, or product pages. This article will guide you on how to get the data from the custom fields you add.

Preparation

This task requires a website run by working WordPress source code. It can be the website installed on the hosting or localhost. If you do not know how to install WordPress, please view this below video

Then you start installing Advance Custom Fields plugin. This plugin installation is quite simple. You just need to download and unzip the plugin folder or install directly by the feature which allows to search plugin in the backend of WordPress.

Usually, the problem with acf get field key is Get field key by field name. The below code will help you solve this problem. 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.

<?php
/**
 * Get field key for field name.
 * Will return first matchedacf field key for a give field name.
 * 
 * ACF somehow requires a field key, where a sane developer would prefer a human readable field name.
 * http://www.advancedcustomfields.com/resources/update_field/#field_key-vs%20field_name
 * 
 * This function will return the field_key of a certain field.
 * 
 * @param $field_name String ACF Field name
 * @param $post_idintThe post id to check.
 * @return 
 */
functionacf_get_field_key( $field_name, $post_id ) {
  global $wpdb;
  $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_name , '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_name;
  }
  // 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;
}
?>

Action

You can readmore about get field at here 

In addition, getting data from the custom fields is quite simple. For example, you have lots of posts about hotels.  At the bottom of each article, there is a hotel host’s telephone number. You can absolutely add the contents in the editor of WordPress. However, it will be easier to style or collect telephone numbers if fields are used here. In the backend, each article will include one textbox for you to input data.

To add a field to the articles, we create a field in AFC’s administration. You should pay attention to the two important parts which are Field name and post type. So, if you just want to add the field to the normal article, this field will not show up in the product pages.

acf get field key

After saving, there will be a textbox in each post:

acf get field key

Now, when creating posts, you can input information in this box. In order to show this box in the front end, you do as the following steps.

You edit the using theme. Posts will be displayed in single.php file by default. You can get to Echo $value = get_field( “phone_number” ) to export the data. Go back to the above figure,phone_number is the field name we have just created. This syntax will help display the content in each page.

If you want to get data of the field by a post ID, we use the following syntax
Echo $value = get_field( “text_field”, 24); in which 24 is the ID of the post you want the data to display.

Because Advanced Custom Fields is simple to use and customize as well, it is an integral tool for people who want to customize WordPress theme.

Summary

In Prestashop, it will be different if you want to add fields to pages such as register or checkout page. These fields are not for admin but for users to input the data. The admin should be a master of code to add a custom form. However, to save time and increase the security when you do not know anything about code, Custom Fields will make everything easier. You just need to drag and drop to choose the suitable fields to add. The system will automatically create fields for you. You can refer to this link to view module custom field .Here you can try the demo for free before deciding to apply the module to your project.

We have finished the article acf get field key. If you feel this article is helpful, please share it. If you any question or recommendation, please leave a comment or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *