To sum things up, I am writing a plugin that gives a Custom Post Type a few default ACF fields and then provides a set of fields for each ‘sibling’ custom post type. I had a prior issue (which is now resolved) that you can view here: https://support.advancedcustomfields.com/forums/topic/acf_add_local_field-not-updating-on-save-when-auto-adding-fields/
The issue now is that, while the fields update on save, any field that is added via acf_add_local_field
is not returning in a post on the front end. Fields are not displaying in get_fields()
or acf_local()
dumps, and cannot be called with get_field()
or the_field
, even when targeted with the post ID.
I am not sure what the issue is. Any help or advice would be appreciated.
I will add code in a reply below!
This code generates ‘default’ fields (ones that appear on every post regardless of how many ‘sibling’ posts there are). The resulting fields in this code will return in a get_fields()
dump:
function Add_ACF_Location_Fields() {
acf_add_local_field_group(
array(
'key' => 'group_1234',
'title' => 'Location Settings',
'fields' => array(
array (
'key' => 'field_1',
'label' => 'Room',
'name' => 'location_compartment_value',
'type' => 'text',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'locations',
),
),
),
)
);
}
And then this code generates ‘sibling’ fields – one set of fields for each ‘sibling’ post. The resulting fields in this code do not return in a get_fields()
dump but do save properly when a post is updated:
function Location_Specific_Fields() {
/**
* Return if trash
*/
if( isset( $_GET['post_status'] ) ){
if( $_GET['post_status'] === 'trash' ) {
return;
}
}
/**
* Get the ID of the post being editted
*/
global $post;
if( isset( $post->ID ) ) {
$edit_ID = $post->ID;
} elseif( isset( $_GET['post'] ) ) {
$edit_ID = $_GET['post'];
} elseif ( isset( $_POST['post_ID'] ) ) {
$edit_ID = $_POST['post_ID'];
} else {
return;
}
// Blank array to hold location field arrays
$fields = [];
// Get all Locations
$args = array(
'post_type' => 'locations',
'posts_per_page' => '800',
'order' => 'menu_order',
'order' => 'ASC'
);
$locs = new WP_Query($args);
// Create fields for each location
if( $locs->have_posts() ) :
while( $locs->have_posts() ) : $locs->the_post();
$cur_id = strval( get_the_id() );
// Do not add fields for current post
if( $cur_id === $post_id ) {
continue;
}
// Unique ID using ids of current post and target post
$field_end_string = $post_id . $cur_id;
// Showing just a text field here for sample purposes, but there
// are other fields that use the same logic.
// *KEYS ARE UNIQUE from field to field but do not change*
$fields[] = array (
'key' => 'field_foo_' . $field_end_string,
'label' => 'Foo',
'name' => 'foo_' . $field_end_string,
'type' => 'text',
'parent' => 'group_1234',
);
// Example Repeater Field
$fields[] = array (
'key' => 'field_bar_' . $field_end_string,
'label' => 'Bar',
'name' => 'bar_' . $field_end_string,
'type' => 'repeater',
'parent' => 'group_1234',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => '',
'layout' => 'row',
'button_label' => 'Add Step'
);
// Example Repeater Sub Field
// Directions SubField: Step
$fields[] = array (
'key' => 'field_bar_step_' . $field_end_string,
'label' => 'Step',
'name' => 'step',
'type' => 'text',
'parent' => 'field_bar_' . $field_end_string,
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
);
endwhile;
endif;
// Iterate through $fields array, add each child array as local field.
foreach ($fields as $field) {
acf_add_local_field($field);
}
}
Both functions are called via add_action( 'acf/init', funcName )
.
If anything needs clarity, please let me know!
Hi @iamhexcoder
I believe I’ve answered your question here: https://support.advancedcustomfields.com/forums/topic/acf_add_local_field-not-updating-on-save-when-auto-adding-fields/#post-41389. The problem is that the $_GET['post']
and $_POST['post_ID']
variables are not available on the front end. Also, the global $post
variable is not available on acf/init
hook, so you need to hook it to wp
for the front end.
I hope this helps 🙂
For future folks, the answers and in depth details are here:
The topic ‘acf_add_local_field saving but not displaying in post’ 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.