Support

Account

Home Forums General Issues Strange Behavior with field data and get_field

Solved

Strange Behavior with field data and get_field

  • I have written an api that is to return back a bunch of ski resort data.

    Here is the function:

    function fs_ski_resorts_data(){
        $args = array(
            'posts_per_page'    => 999,
            'post_type'         => 'fs_ski_resorts',
        );
        $the_query = new WP_Query( $args );
        $fs_ski_resorts = get_posts($args);
        $fs_ski_resorts_data = [];
        $i = 0;
        while( $the_query->have_posts() ) : $the_query->the_post();
              // i have removed all but 1 field to minimize example
              $variable = get_field('fs_ski_resort_data_fs_aa_zoom');
              $fs_ski_resorts_data[$i]['ZOOM']  = $variable ;
              $i++;
        endwhile;
        return $fs_ski_resorts_data;
    }
    

    the fs_ski_resort_data_fs_aa_zoom field is a number. I have 265 records and all of them have a number, 15, however, some return $fs_ski_resorts_data[$i][‘ZOOM’] as NULL and some with 15, as expected. When I enter the view the record for the null ones, I can see 15 is there.

    I have found that if I go to the record with null value from the above function and click UPDATE and then go back and run the above function, the number is there. It is like the number is there but isn’t at the same time. Just wondering what could possibly be the problem.

  • Interesting. This is the record BEFORE I go to the backend and hit update.

    {
    “osm_geometry”: “way/955280979”,
    “fs_ski_resort_name”: “牧の入高原スノーパーク”,
    “fs_ski_resort_name_en”: “Makinoiri Kogen Snow Park”,
    “fs_ski_resort_landuse”: “winter_sports”,
    “fs_ski_resort_sport”: “downhill”,
    “fs_ski_resort_lit”: null,
    “fs_ski_resort_email”: null,
    “fs_ski_resort_phone”: null,
    “fs_ski_resort_fax”: null,
    “fs_ski_resort_addr_full”: null,
    “fs_ski_resort_postal_code”: null,
    “fs_ski_resort_contact_website”: “https://makinoiri.com/”,
    “fs_ski_resort_contact_facebook”: null,
    “fs_ski_resort_contact_twitter”: null,
    “fs_ski_resort_wikidata”: null,
    “fs_ski_resort_wikipedia”: null,
    “fs_ski_resort_aa_winter_sports_sq_km”: “0.74”,
    “fs_ski_resort_aa_pistes_length”: “5660”,
    “fs_ski_resort_aa_lifts_length”: “2520”,
    “fs_ski_resort_aa_pistes_count”: “12”,
    “fs_ski_resort_aa_lifts_count”: “4”,
    “fs_ski_resort_aa_min”: “610”,
    “fs_ski_resort_aa_max”: “891”,
    “fs_ski_resort_aa_novice_length”: “0”,
    “fs_ski_resort_aa_novice_count”: “0”,
    “fs_ski_resort_aa_easy_length”: “0”,
    “fs_ski_resort_aa_easy_count”: “0”,
    “fs_ski_resort_aa_intermediate_length”: “0”,
    “fs_ski_resort_aa_intermediate_count”: “0”,
    “fs_ski_resort_aa_advanced_length”: “0”,
    “fs_ski_resort_aa_advanced_count”: “0”,
    “fs_ski_resort_aa_expert_length”: “0”,
    “fs_ski_resort_aa_expert_count”: “0”,
    “fs_ski_resort_aa_freeride_length”: “0”,
    “fs_ski_resort_aa_freeride_count”: “0”,
    “fs_ski_resort_aa_extreme_length”: null,
    “fs_ski_resort_aa_extreme_count”: null,
    “fs_ski_resort_aa_zoom”: null,
    “fs_ski_resort_aa_y”: “36.8143696397”,
    “fs_ski_resort_aa_x”: “138.3968575196”,
    “fs_ski_resort_aa_mean”: “192.00350194703”,
    “fs_ski_resort_aa_pitch”: null,
    “ZOOM”: null
    },

    This is the output after I hit the UPDATE button. The info is sitting populated in the input boxes. Strange.

    {
    “osm_geometry”: “way/955280979”,
    “fs_ski_resort_name”: “牧の入高原スノーパーク”,
    “fs_ski_resort_name_en”: “Makinoiri Kogen Snow Park”,
    “fs_ski_resort_landuse”: “winter_sports”,
    “fs_ski_resort_sport”: “downhill”,
    “fs_ski_resort_lit”: “”,
    “fs_ski_resort_email”: “”,
    “fs_ski_resort_phone”: “”,
    “fs_ski_resort_fax”: “”,
    “fs_ski_resort_addr_full”: “”,
    “fs_ski_resort_postal_code”: “”,
    “fs_ski_resort_contact_website”: “https://makinoiri.com/”,
    “fs_ski_resort_contact_facebook”: “”,
    “fs_ski_resort_contact_twitter”: “”,
    “fs_ski_resort_wikidata”: “”,
    “fs_ski_resort_wikipedia”: “”,
    “fs_ski_resort_aa_winter_sports_sq_km”: “0.74”,
    “fs_ski_resort_aa_pistes_length”: “5660”,
    “fs_ski_resort_aa_lifts_length”: “2520”,
    “fs_ski_resort_aa_pistes_count”: “12”,
    “fs_ski_resort_aa_lifts_count”: “4”,
    “fs_ski_resort_aa_min”: “610”,
    “fs_ski_resort_aa_max”: “891”,
    “fs_ski_resort_aa_novice_length”: “0”,
    “fs_ski_resort_aa_novice_count”: “0”,
    “fs_ski_resort_aa_easy_length”: “0”,
    “fs_ski_resort_aa_easy_count”: “0”,
    “fs_ski_resort_aa_intermediate_length”: “0”,
    “fs_ski_resort_aa_intermediate_count”: “0”,
    “fs_ski_resort_aa_advanced_length”: “0”,
    “fs_ski_resort_aa_advanced_count”: “0”,
    “fs_ski_resort_aa_expert_length”: “0”,
    “fs_ski_resort_aa_expert_count”: “0”,
    “fs_ski_resort_aa_freeride_length”: “0”,
    “fs_ski_resort_aa_freeride_count”: “0”,
    “fs_ski_resort_aa_extreme_length”: “”,
    “fs_ski_resort_aa_extreme_count”: “”,
    “fs_ski_resort_aa_zoom”: “15”,
    “fs_ski_resort_aa_y”: “36.8143696397”,
    “fs_ski_resort_aa_x”: “138.3968575196”,
    “fs_ski_resort_aa_mean”: “192.00350194703”,
    “fs_ski_resort_aa_pitch”: “85”,
    “ZOOM”: “15”
    },

  • You ticket systems is freaking out. I get blocked when I try to add info. Now I just wrote a couple paragraphs and all gone.

    Do you have an email I can write to please?

  • I have found that if I go to the record with null value from the above function and click UPDATE and then go back and run the above function, the number is there. It is like the number is there but isn’t at the same time. Just wondering what could possibly be the problem.

    When this happens it is usually because the field was updated incorrectly. If a field is updated using update_field() and the field for the post it is attached to does not have a value in the database then you must use the field key when calling update_field(). You cannot use the WP function update_post_meta() or any equivalent function to update fields that do not already exist in the database. If you do then you must also add the field key reference to the database yourself.

    See this section of the update_field() documentation for an explanation of field key reference https://www.advancedcustomfields.com/resources/update_field/#updating-via-field-key

  • Thank you for your help. I missed that part in the man page.

    I reviewed how I am updating records. This is how I am doing it;

    update_field($acf_field_name, $value, $post_id);

    The man page says $acf_field_name is the field name or field key.

    Now I see later in the man page, it says under “Updating via Field Key”,

    The field’s key should be used when saving a new value to a
    post (when no value exists). This helps ACF create the correct
    ‘reference’ between the value and the field’s settings.

    So only if the field has an existing value, can I use the acf name? If there is no value, (eg NULL), then I must use the KEY instead? Am I correct in saying that if one doesnt know if the field contains a value or not, then best to ALWAYS use the KEY?

    May I suggest an update to the man page at the top where it says, “is the field name or field key” to read “is the field name or field key, however, when updating new values, a KEY must be used instead of the name (see more below)”.. or something like that?

    BTW, I have fields in groups. For names I would need to concantenate group-name with field-name. I assume with fields, no need to do that, right?

    Thank you again.

  • BTW, null mean no value, however, how about “” (empty string). That will count as a value, correct?

  • Actually, yes, when using update_field() is is best to assume that the field has no value and always use the field key.

    The documentation should actually say that in my opinion, but not much I can do about that.

    Any value in the database, even an empty string. What matters is that ACF creates two entries in the database, one has a meta_key of your field name and the other has a meta_key of your field name prepended with an underscore _{$field_name}. If both of these exist then you can use the field name. ACF actually uses the field key for everything because they are unique. Field names are only provided for our convenience. If you look at an admin page with fields and inspect you will find that all of the inputs use the field key for the field name.

    The only time this is not the case is when using an acf/save_post with a priority of > 10 and you can be sure that ACF has already created a DB entry for the field and you intend to change it for some reason.

  • @John Huebner I very much appreciate your detailed replies. This has cleared up my misunderstanding. Thank you!

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

You must be logged in to reply to this topic.