Home › Forums › Feature Requests › Inserting fields programatically
Hi,
It would be nice to have an official way to insert fields to a custom post type programatically.
Use case: we are importing events from a remote API to our local DB for a custom post type and we will associate it with ACF fields. I can figure out how to insert them, but if there is an “update_field()” method, why there is no “insert_field()”?
best
Hi @bela
I think you want to have a look at this documentation:
http://www.advancedcustomfields.com/resources/register-fields-via-php/
You can create fields and fieldgroups dynamically with PHP using that API.
You can also create your own acf-json file as well and save in the theme if you prefer that.
http://www.advancedcustomfields.com/resources/local-json/
Hello, I want to store data in my ACF field structure. I used these two lines:
add_post_meta( $insert_id, $field, $value );
add_post_meta( $insert_id, '_'.$field, $acf_fields[ $field ] );
Okay,
that might save the values to the post but if you don’t have any fields that are actually using those values you wont see them in wp admin.
I wrote error tracking that parses the ACF JSON-s so fields must match, otherwise it explodes 🙂
Ah okay.
Well I don’t see what exactly you want.
The update_field
will work as long as there are actual fields to the content you want to add them to, they don’t need to already have content.
And to create fields completely from scratch you’d use the PHP functions already available like acf_add_local_field()
.
There’s no point in a insert_field()
function as it would either do the same as acf_add_local_field()
or just add meta to content. In the latter case it wouldn’t be helpful since there’s no actual ACF field to go with it.
There is however a plan to create a new api function for inserting new rows into a repeater or flexible field. That is currently a bit tricky (but doable) and it will be much easier!
Ok, got it I haven’t seen acf_add_local_field()
in the docs. Maybe mention it on pair with in the update_field() doc. (maybe it’s there, I’m lazy to look it up)
Okay 🙂
You can read all about it in the link i posted earlier: http://www.advancedcustomfields.com/resources/register-fields-via-php/
Best of luck in your project!
The topic ‘Inserting fields programatically’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.