ACF PRO version 5.8.6, WP version 5.2.4.
I’ve created field “Speakers”:
When I’m updating field in admin (two users added), everything works correctly:
– DB value:
a:2:{i:0;s:1:"1";i:1;s:3:"101";}
– field admin HTML:
<input type="hidden" id="acf-field_5ce2cd297ade8-input" name="acf[field_5ce2cd297ade8]"/>
<select
id="acf-field_5ce2cd297ade8"
class=""
name="acf[field_5ce2cd297ade8][]"
data-ui="1"
data-ajax="1"
data-multiple="1"
data-placeholder="Select"
data-allow_null="1"
multiple="multiple"
size="1"
>
<option value="1" selected="selected" data-i="0">master (Test Test)</option>
<option value="101" selected="selected" data-i="1">robot</option>
</select>
– UI shows two users:
BUT, when field is update using update_field(), admin UI shows empty value.
– PHP code:
update_field( 'speakers', array( 1, 101 ), $post_id );
– DB value (same as updating field in admin!):
a:2:{i:0;s:1:"1";i:1;s:3:"101";}
– field admin HTML (same as updating field in admin!):
<input type="hidden" id="acf-field_5ce2cd297ade8-input" name="acf[field_5ce2cd297ade8]"/>
<select
id="acf-field_5ce2cd297ade8"
class=""
name="acf[field_5ce2cd297ade8][]"
data-ui="1"
data-ajax="1"
data-multiple="1"
data-placeholder="Select"
data-allow_null="1"
multiple="multiple"
size="1"
>
<option value="1" selected="selected" data-i="0">master (Test Test)</option>
<option value="101" selected="selected" data-i="1">robot</option>
</select>
– UI empty:
Is it bug, or am I missing something?
BTW, in both cases
get_field( 'speakers', $post_id );
returns
Array ( [0] => 1 [1] => 101 )
Are you attempting to update the field for a post that does not have a value previously set using the admin?
If this is the case then you need to use the field key instead of the field name when calling update_field()
.
Yes, I did test with update_field()
using field key – same outcome.
I’m actually stumped because I don’t see any reason why it shouldn’t be working.
Hello,
Did you find why this was happening?
I have the exact same issue, in the admin the select seems to work since the option has the selected attribute:
<select id="acf-field_5e78f48412b5e" class="select2-hidden-accessible" name="acf[field_5e78f48412b5e][]" data-ui="1" data-multiple="1" data-placeholder="Choisir" data-allow_null="0" multiple="" size="1" tabindex="-1" aria-hidden="true">
<option value="3" selected="selected" data-i="0">jason.g@….ch</option>
</select>
But the select2 doesn’t work, and when saving again from the admin, it resets the value.
Thank you,
Jason
The topic ‘User Field (Relational), bug when updating field using update_field()’ 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.