I believe with get_sub_field_object
you have to pass the field key, which is different than the field name.
Info on get_sub_field_object is here: https://www.advancedcustomfields.com/resources/get_sub_field_object/
Info on displaying field keys is here:
https://thestizmedia.com/show-field-keys-acf-pro/
Thanks a ton for this trick, it worked great!
I took the liberty of mashing this function up with a Storm UK Gravity Forms select plugin, and made a Formidable Forms select plugin:
https://github.com/iamhexcoder/formidable_select
Just thought you’d like to know! Any feedback or issues, let me know.
Props given to @nicmare and @welaika in the README 😉
Hi @john, I appreciate the response.
I’m not quite sure what you mean…the ACF get_field
functions are coming from code that I wrote in a walker that gets called in wp_nav_menu()
in header.php
and an image object in footer.php
. If I was to edit the plugin templates, what would I be looking to edit?
Additionally, I was in contact with the plugin author, who said the issue may be from “the attempt to bootstrap a post object”…which I am a little lost on as well, but thought might be helpful here.
If it seems best/fastest route, I may just update how the ACF field values are pulled in with custom functions (switch image object to ID, etc.)
Each post is a physical location, and the client needs to create directions between each of the locations. So there needs to be a set of direction from location 1 to locations 2 and 3, from location 2 to 1 and 3, etc.
Things will be a bit messy, but it’s the data that is needed!
Thanks again for all of your help!!
Cheers
Shaun
For future folks, the answers and in depth details are here:
Thanks @James
Sorry for the double post, thought it might be a separate enough issue to warrant it’s own post.
This works great! Thanks so much.
One more question, if you don’t mind. Let’s say that there end up being 100-200 locations
posts. Is this a crazy thing to do? Will the query really drag things out, or will there end up being so many fields that things will get really gunked up?
Thanks so much, I appreciate all of your help!
Cheers
Shaun
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!
This is now working and all of the fields are saving correctly, however now none of the fields added with acf_add_local_field
are showing up for get_fields()
. Any ideas as to why?
Ermergerd, you’re a lifesaver @james!
Thanks
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.