Support

Account

Home Forums Backend Issues (wp-admin) From Where Does "get_field()" Acquire A Value?

Unread

From Where Does "get_field()" Acquire A Value?

  • The ACF documentation states the following in its description of the “get_field()” function:

    “Returns the value of a specific field.”

    In the interest of clarification, is a custom field’s value retrieved via a database query or is it acquired directly from the current value populated for that custom field in a post’s editor?

    If it the value results from a database query, then I need to understand why the value for one of my custom fields is not saving properly to the WP postmeta table. The following are code excerpts from two ACF custom field types that I created. Each instance of the custom field types are used within the same custom post type. The first custom field type’s value saves properly to the WP postmeta table; the second one does not.

    ===================================
    First ACF custom field code excerpt
    ===================================

    function __construct($settings) {
      // Stuff...
      $this->defaults = array(
        'value' => 'Testing 1...2...3...'
      };
      // More stuff...
    }
    
    function render_field_settings($field) {
      acf_render_field_settings($field, array(
        'label' => __('The Test', 'acf'),
        'instructions => __('An ACF test case value', 'acf'),
        'type' => 'text',
        'name' => 'value'
      ));
    }
    
    function render_field($field) {
      ?>
      <label for="<?php echo esc_attr($field['name']) ?>">ACF Test Case Value</label>
      <input
        type="text"
        name="<?php echo esc_attr($field['name']) ?>"
        value="<?php echo esc_attr($field['value']) ?>" />
      <?php
    }

    ===================================
    First ACF custom field code excerpt
    ===================================

    function __construct($settings) {
      // Stuff..
      $this->defaults = array(
        'value' => array(
                     'host' => 'uvi',
                     'version' => '1.0'
                   )
      );
      // More stuff...
    }
    
    function render_field_settings($field) {
      acf_render_field_setting($field, array(
        'label' => __('Plugin Host Value', 'acf-plugin-host'),
        'instructions' => __('Enter the plugin host & version values in this form', 'acf'),
        // 'type' => 'text',
        'type' => 'array',
        'name' => 'value'
      ));
    }
    
    function render_field($field) {
      ?>
      <label for="<?php echo 'pc-host-' . $field['value']['host']; ?>">Plugin Host</label>
      <input
        type="text"
        id="<?php echo 'pc-host-' . $field['value']['host']; ?>"
        name="<?php echo 'pc-host-' . $field['value']['host']; ?>"
        value="<?php echo $field['value']['host']; ?>">
      </input>
    
      <label for="<?php echo 'pc-host-version-' . $field['value']['version']; ?>">Plugin Host Version</label>
      <input
        type="text"
        id="<?php echo 'pc-host-version-' . $field['value']['version']; ?>"
        name="<?php echo 'pc-host-version-' . $field['value']['version']; ?>"
        value="<?php echo $field['value']['version']; ?>">
      </input>  
      <?php
    }

    As I understand it, the ACF custom field value is saved to the WP postmeta table when the edited post is saved/updated; saving the custom field data requires no additional code by default. The first custom field value is saved properly, but this is not the case with the second custom field value. For the second case, the WP postmeta table stores a “NULL” value.

    The primary difference between the first and second custom field types is that the first’s value is scalar but the second’s value is an array. For the second custom field type’s code, I commented out the type text value within the “render_field_settings()” function and assigned the type key to “array” instead.

    Unfortunately, that change had no impact on the value saved to the WP postmeta table. It still saves as “NULL”.

    Your feedback is appreciated.

    Thank you.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.