Home › Forums › General Issues › Add local field issue
Hi. I’m trying to generate some fields with acf_add_local_field. Beforehand I build up an array to loop through and add each local field. The array in the first foreach is based on values set in another ACF options page. When I loop through the 2nd foreach with just the initial $fields array. It all works fine. When I add the items to the $fields array from the first foreach. It breaks and no fields are being generated. Although the print_r shows the new items are listed the same way as the first two. My questions is mainly. Why does it break and how can I fix this. (I know the code is less efficient as it should be).
function my_acf_add_local_field_groups() {
$fields = ['test', 'test2'];
foreach(get_mockup_objects() as $key => $object){
if($object['hasPlaceholder']) {
$fields[] = $object['placeholder']['id'];
}
}
foreach($fields as $field) {
acf_add_local_field([
'label' => $field,
'name' => $field,
'type' => 'post_object',
'parent' => 'field_5eac34065e6c1',
'post_type' => 'project'
]);
}
}
add_action('acf/init', 'my_acf_add_local_field_groups');
function get_mockup_objects(){
$homepageFields = get_fields(get_option('page_on_front', true));
$mockups = get_fields('mockups');
$selectedMockup = '';
foreach($mockups['homepage'] as $mockup){
if($homepageFields['hero']['mockupHomepage'] === $mockup['id']){
$selectedMockup = $mockup['objects'];
}
}
$temp = [];
foreach($selectedMockup as $object){
if($object['hasPlaceholder']){
$temp[] = $object;
}
}
return $temp;
}
This is the return of the get_mockup_objects() function:
Array
(
[0] => Array
(
[coordinates] => Array
(
[top] => -2
[left] => 50
)
[hasPlaceholder] => 1
[placeholder] => Array
(
[size] => Array
(
[width] => 776
[height] => 417
)
[id] => Ac1kkPPwdg
)
)
[1] => Array
(
[coordinates] => Array
(
[top] => 349
[left] => 142
)
[hasPlaceholder] => 1
[placeholder] => Array
(
[size] => Array
(
[width] => 422
[height] => 265
)
[id] => TZeotr4IRH
)
)
)
Anyone any idea’s? Thanks in advance!
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!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.