Home › Forums › ACF PRO › acf_add_local_field saving but not displaying in post › Reply To: acf_add_local_field saving but not displaying in post
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!
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.