Hi guys,
I have three field groups, each group is added to a different custom widget. Each group have different fields, but also same fields such as Title and Intro Text.
For example, the name of title and text fields in all field groups are my_widget_title
and my_widget_text
.
Is it safe to use such method or is it better to give different names in each field group?
Thank you.
Hi @vasili
It should be safe. But, to avoid any confusion in the future, I suggest you to use a different name instead.
Thanks 🙂
Hi James,
Thank you for your reply.
By “confusion” you mean the human factor, not DB conflicts?
Hi @vasili
ACF will use field keys to save the data in the database. And because you use the fields in widgets, I believe retrieving the value won’t be an issue either because you need to provide the widget IDs. So yes, it’s more of a human factor.
I hope this makes sense 🙂
Hi James,
This makes sense and more than this, it seems so obvious and simple. Really appreciate your help. Thank you.
@acf-support forgive me for resuming an old post, but it would seem the fields get confused script-wise if they have the same name.
I encountered the need to copy all ACF fields from a group_a
of a post type type_a
to a group_b
of a post type type_b
due to an analysis mistake.
So, let’s consider this code snippet
if ($tecnologie = get_field('tecnologie', $post->ID)) {
$tecnologie_new = '<ul>';
foreach ($tecnologie as $tecnologia) {
$tecnologie_new .= '<li>';
$tecnologie_new .= $tecnologia['tecnologia'];
$tecnologie_new .= '</li>';
}
$tecnologie_new .= '</ul>';
update_field('tecnologie', $tecnologie_new, $new_id);
}
Here, $post
is of type type_a
and $new_id is the ID of a new post I created of type type_b
. In both field groups there’s a field called tecnologie
, which is a Repeater in the first type and a WYSIWYG Editor in the second. I basically need to turn the repeated rows into a <ul>
tag.
Post type_a
field group, field tecnologie
:
Post type_b
field group, field tecnologie
:
The above script returns an error:
( ! ) Warning: Invalid argument supplied for foreach() in (…)\wp-content\plugins\advanced-custom-fields-pro\pro\fields\class-acf-field-repeater.php on line 844
Because the type of field the plugin considers is the Repeater, despite the required update is on the WYSIWYG.
@acf-support forgive me for resuming an old post, but it would seem the fields get confused script-wise if they have the same name.
I encountered the need to copy all ACF fields from a group_a
of a post type type_a
to a group_b
of a post type type_b
due to an analysis mistake.
So, let’s consider this code snippet
if ($tecnologie = get_field('tecnologie', $post->ID)) {
$tecnologie_new = '<ul>';
foreach ($tecnologie as $tecnologia) {
$tecnologie_new .= '<li>';
$tecnologie_new .= $tecnologia['tecnologia'];
$tecnologie_new .= '</li>';
}
$tecnologie_new .= '</ul>';
update_field('tecnologie', $tecnologie_new, $new_id);
}
Here, $post
is of type type_a
and $new_id is the ID of a new post I created of type type_b
. In both field groups there’s a field called tecnologie
, which is a Repeater in the first type and a WYSIWYG Editor in the second. I basically need to turn the repeated rows into a <ul>
tag.
Post type_a
field group, field tecnologie
:
Post type_b
field group, field tecnologie
:
The above script returns an error:
( ! ) Warning: Invalid argument supplied for foreach() in (…)\wp-content\plugins\advanced-custom-fields-pro\pro\fields\class-acf-field-repeater.php on line 844
Because the type of field the plugin considers is the Repeater, despite the required update is on the WYSIWYG.
Not entirely
If you have a CF called “Hero CF” with a field “main_title”,
and a CF called “Intro CF” with a field “main_title”,
and then apply those both to a page/post, it can’t distinguish the fields.
(I believe updating the second field in the admin, will modify the first as well…since the field for both is still “main_title”)
In which case I think it’s probably better to give each CF a containing group (hero, intro) so you end up with fields eg “hero_main_title” and “intro_main_title” (but can still be accessed by “main_title” in code for that Group)
Anyone have better suggestion?
Thanks.
The topic ‘Is it safe to use the same field name in different field groups’ 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.