Support

Account

Home Forums General Issues How to get field reference

Solved

How to get field reference

  • I have several custom fields for a custom post type. I need to populate all of them at once via php (meaning one call to populate all of them, and not one update_field() call per field), otherwise it’s getting very slow to add new posts.

    When looking at the wp_postmeta table in the database, I can see the fields there of posts that I have manually created, and for each field there seems to be 2 rows, one with the field name and its value, an another one with something like _field_name as key and something like field_2342asdasd234 as value. I believe that last one is what’s it’s referred to as field reference.

    Looks like I should be able to add multiple values in that table at once via SQL, but the thing is that I can’t find anywhere how to get the field reference for each field that I have.

    Is there anywhere I can see those field references? Maybe via the admin or the database? Or programmatically somehow?

    My idea is to do something like what’s explained here https://support.advancedcustomfields.com/forums/topic/better-way-to-update-multiple-fields-update_field/ , but I don’t know where they get the field reference. Any help would be much appreciated.

  • Field keys are available in the admin when editing the field group, there is a checkbox under screen options.

    There really isn’t a reliable way to get the field key using PHP. Doing so requires that the field is already in the DB using get_field_object(‘FIELD NAME’). This will not work if the field does not already have a value.

  • Wow this is exactly what I was looking for! Thanks so much!

    Yes I tried using get_field_object(‘FIELD NAME’) and run into the problem you mention.

    I was just wondering though, if those field keys appear in the admin when editing a field group, they MUST be somewhere in the database right? Maybe wp_options or wp_postmeta… I’ve dug around a bit without success unfortunately, do you know by any chance where those could be stored?

  • When editing a field group ACF loads the field group (post) then loads the fields (child posts) for the group. The field keys are part of the post and when editing a field group everything is dependent on the field key.

    When editing content, again, all editing is done by field key.

    The field name is really just a convenience for us to make it easier when coding or if you want to use a function like get_post_meta() to get the field value.

    Yes, if a value is save to a field for a post there will be an entry in the DB that relates that field to the post, but this is so that ACF can determine the unique field object since field names do not need to be unique.

  • Gotcha, with that info about child posts I’ve done some digging and found EXACTLY what I was looking for.

    If I get the post_id of the field group post, and search in the wp_posts table for the posts whose parent_id is that post_id, I get the list of all the posts (fields) for that field group with their field name (post_excerpt) and field key (post_name).

    This will allow me to programmatically get a list of all the fields and their corresponding field key for a specific field group, and use this info to save in the wp_postmeta table all the fields of a post in one call, which is my ultimate goal to speed things up.

    Thanks so much for your help!

  • I wish you luck with this. I have had my share of problems with slow updating. My main issue is with flexible content fields and I have never found a way to reliably bypass the ACF saving and do the updates myself.

    How are you preventing ACF from saving values in order to run your own code to do the saving?

  • I actually haven’t used flexible content fields yet, so I haven’t run into that issue for now. You’re saying that ACF saves stuff even if you directly store the fields yourself in the database?

  • Yes, when you hit submit on a page with ACF fields ACF will save all of those fields unless you do something to stop that from happening. You are following advice that I gave 4 years ago when I knew less about ACF’s save process. At that time I actually did try to build something but I could not figure out how to bypass ACF saving the values anyway or how to deal with many of the field types.

  • Ah gotcha, I’m actually doing everything in the back end programmatically, I’m not using ACF on forms in the front end, so that’s probably why I haven’t run into this issue

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

You must be logged in to reply to this topic.