Previously I used ACF only in back-end & output fields in front-end with get_field(). I used acf-hooks. All was fine.
But in the current project I use acf_form(). All my data are two types: for a user and for an admin. So, I separate them for “fields_group_admin” & “fields_group_user”. The user can edit content. The admin or PHP-code can edit “engine-data”, for example like terms of custom taxonomy “request_status”.
THE ROOT OF THE PROBLEM with acf_form() + update_field():
update_field('_status', $status_term_obj->term_id, $post_id); // $value = 5
$field_groups = array( 'group_5729d360b78eb', // Request Admin Part is visible = all is work (I didn't touch admin-fields in front-end, fields were updated in my code in hook "acf/save_post" with priority 20!) 'group_5729dc812096d', // Request User Part );
$field_groups = array( //'group_5729d360b78eb', // Request Admin Part is hidden = don't work!! (but, work field of User-type) 'group_5729dc812096d', // Request User Part );
Please, look at the picture: http://prntscr.com/b1a0yd
On the top is “the working case”, on the bottom is “the not working case”. I changed just one string in code: I commented “Request Admin Part” and it’s hidden them in front-end.
In the Debuger it looks that:
1) start: http://prntscr.com/b1a2ew – all is Ok
2) inside: http://prntscr.com/b1a2lm – the same
3) updated: http://prntscr.com/b1a2y1 – but the “update reference” has $value=””
4) “return != false”: http://prntscr.com/b1a3i5 – but:
5) in wp-admin post is that: http://prntscr.com/b1a43z – don’t work!
Looks like some a wrong updating of the field. I don’t understand why it is.
1) How must I properly hide admin\code-filds? (This fields I don’t show to users in front-end, but I need them in wpa-admin and for working site).
2) How and where must I properly update admin\code-filds? Maybe “acf/save_post” not a good place for update_field() for the new acf_form() post.
I must use keys, like “field_5729dc2c38f13”, instead names of fields.
The solution for hidden in acf_form() fields (By “hidden” I mean that I don’t show it the field to user in front-end):
update_field('_status', $status_term_obj->term_id, $post_id); // don't work update_field('field_5729dc2c38f13', $status_term_obj->term_id, $post_id); // work!
I hope that will help someone.
At final I reject acf_form() because I don’t know how can I use it with hidden fields.
I think ACF need the setting “Hidden in front-end: true/false” and the parameter acf_form() “input_init_values” => $init_values.
For example, how must I do with acf_form():
Question post about some item:
1) item_id (hidden for user in front-end)
Without acf_form() it will be:
<form action="<?php echo get_stylesheet_directory_uri() ?>/question-update.php" method="POST"> <input type="hidden" name="item_id" value="<?php echo get_the_ID() ?>" /> <input type="text" name="title" value="" /> <input type="textarea" name="message" value="" /> </form>
I believe it’s because the database doesn’t have the reference key for the new custom field entries. This page should give you more idea about it (under the Basic (field key) section): https://www.advancedcustomfields.com/resources/update_field/#usage. To fix it, you need to use the field key instead of the field name like your solution.
If you want to show certain the field groups or custom fields, I think you can use the “field_groups” and “fields” options. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acf_form/.
I hope this helps 🙂
You must be logged in to reply to this topic.
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!