0

Uncaught TypeError: Cannot access offset of type string on string in C:\OSPanel\domains\wordpress\wp-content\plugins\advanced-custom-fields-pro\includes\acf-value-functions.php:63 Stack trace: #0

code this function:

function acf_get_value( $field, $post_id = 0 ) {
    
    // Allow filter to short-circuit load_value logic.
    $value = apply_filters( "acf/pre_load_value", null, $post_id, $field );
    if( $value !== null ) {
        return $value;
    }
    // Get field name. 
    $field_name = $field['name']; // --- HERE ERROR ---
    
    // Check store.
    $store = acf_get_store( 'values' );
    if( $store->has( "$post_id:$field_name" ) ) {
        return $store->get( "$post_id:$field_name" );
    }
    
    // Load value from database.
    $value = acf_get_metadata( $post_id, $field_name );
    
    // Use field's default_value if no meta was found.
    if( $value === null && isset($field['default_value']) ) {
        $value = $field['default_value'];
    }
    
    /**
     * Filters the $value after it has been loaded.
     *
     * @date    28/09/13
     * @since   5.0.0
     *
     * @param   mixed $value The value to preview.
     * @param   string $post_id The post ID for this value.
     * @param   array $field The field array.
     */
    $value = apply_filters( "acf/load_value", $value, $post_id, $field );
    
    // Update store.
    $store->set( "$post_id:$field_name", $value );

    // Return value.
    return $value;
}

$field = get_field_object('name');
$field_name = $field['name'];

not working

1
  • I suggest doing a var_dump($field) just before the line causing the error. What does it show? Commented Apr 2, 2023 at 19:07

1 Answer 1

1

The error means you are trying to get a value from an associative array using a key that does not exist.

Based on the line you identified, change the code to:

// Get field name. 
$field_name = $field['name'] ?? '';

This is for php 7 and above.

If you are using a php version below 7, change the line to

$field_name = $field['name'] ?: '';

However, like the first comment suggested, do a var_dump($field) to see the contents of the array. The above code will only set a default value instead of giving a fatal error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.