Support

Account

Home Forums Backend Issues (wp-admin) get_field() – strange behaviour

Solving

get_field() – strange behaviour

  • Hi there,

    we are experiencing a very strange behaviour with get_field() method.

    In our development environment (PHP 7.0.3) we wrote this hook to solve an issue with WPML

    function mlt_wpml_fix_galleries($post_id, $post, $update)
    {
        global $sitepress;
    
        $post_type = get_post_type($post_id);
        $current_language = $sitepress->get_current_language();
        $languages = apply_filters( 'wpml_active_languages', null, null );
        $field_name = 'gallery-images';
    
        foreach ($languages as $lang => $lang_info) {
            $translated_post_id = apply_filters('wpml_object_id', $post_id, 'photogallery', false, $lang);
            if ('photogallery' == $post_type && $translated_post_id && $lang != $current_language) {
                $values = get_field($field_name, $post_id, false);
                if ($values) {
                    $updated_ids = [];
                    foreach ($values as $value) {
                        $updated_ids[] = apply_filters('wpml_object_id', $value, 'attachment', true, $lang);
                    }
                    update_field($field_name, $updated_ids, $translated_post_id);
                }
            }
        }
    }
    add_action( 'save_post', __NAMESPACE__ . '\\mlt_wpml_fix_galleries', PHP_INT_MAX, 3 );

    It works.

    We did deploy that code on our production environment (PHP 7.1.1) and… get_field() returns false (boolean). We have same code, some database, same plugins, etc… We now solved using:

    $values = get_post_meta($post_id, $field_name, true);

    but I am not able to understand what’s happened.

    Anyone could help?

    My best

  • If you are trying to get the images from the translated post, shouldn’t your get_field() call look something like this

    
    $values = get_field($field_name, $translated_post_id, false);
    
  • Hi @hube2,

    thank you for your reply. I know my english is orrible, I am sorry. Try to better explain myself.

    In our local environment (same db as production, same code (GIT), same plugin, etc…):

    $values = get_field($field_name, $post_id, false);

    it works. It returns an array. On our production it returns false WTF.

    PHP version is just the one difference beetwen environment.

    For your information:

    $post_id is the current language POST ID, so it is right. We are getting values from the current POST ID and copy it to the translated posts, because we want the same exactly gallery, even if user upload photos to german version (for example) and not into english version.

    Best regards

  • Is it not getting the values or not updating the values to the translated post?

  • @hube2 is it not getting the values. get_field() returns FALSE instead of an array.

    Once again, I solved the issue using the native function get_post_meta(), but it is a little bit strange behaviour.

  • Anyone could suggest an easy way to try debug the issue?

  • I can’t really give you a better way to debug because I cannot recreate the issue that you’re seeing.

    Is get_field() returning and empty array, false or NULL, there is a difference. false could mean that the field/data exists but is empty, although I would expect it to be returning an empty array. NULL would mean that the value/data does not exist (is not present in the database) for the post.

  • Thank you @hube2, as I wrote in my previous posts, get_field() returns FALSE, just in our production environment with PHP 7.1.x

  • Like I said, if there is not value in the database then the value returned should be NULL. Since it’s returning false, then there is a value in the database.

    The value is stored as a serialized array. The only reason that a field stored as a serialized array would return false is that the string is not unserializeable, see this http://php.net/manual/en/function.unserialize.php.

    What this means is one of 2 things

    1. The data inserted into the database when you move from dev to production is being altered
    2. There is a difference in the way the two versions of php serialize and unserialize
  • @hube2 I think you’re totally right:

    7.1.0 The allowed_classes element of options) is now strictly typed, i.e. if anything other than an array or a boolean is given, unserialize() returns FALSE and issues an E_WARNING.

  • ACF calls maybe_unserialize after getting the value. It seems that this could be a problem with WP compatibility with PHP 7.1

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘get_field() – strange behaviour’ is closed to new replies.