Home › Forums › ACF PRO › load_field › Reply To: load_field
I’m sorry I didn’t understand how to add the code so that it shows up in a code box. So above it appears to be added as text. Here is the code…
// The key value would be given as a variable for the current post field(s) but I have hardcoded it here for simplicity.
add_filter(‘acf/load_field/key=field_5bf31d5e8e1eb’, function ( $field ) {
// Get the Relationship field’s related post ID from the active Artwork post when in the admin screen (this ACF Relationship field is limited to only one selection so there will only be one related post.)
// When in the Artwork CPT admin screen for post 14341 and using the post ID 14341 it works but when using ‘false’ as the current Artwork CPT admin post it DOES NOT WORK!
$related_ids = get_field( ‘ARTrelationship_SRCrelationship’, false ); //Will not work (it removes the defualt selection and replaces them with ‘No results found’)
$related_ids = get_field( ‘ARTrelationship_SRCrelationship’, 14341 ); //Works and adds ‘Lithograph’ and ‘Etching’
// Get the ID of the one related post.
$related_source_id = $related_ids[0]->ID;
//Create array to hold selection options.
$inheritedvalues = array();
// Count how many repeater fields are present in SRCartwork_types for the one related Source post.
$count = intval( get_post_meta( $related_source_id, ‘SRCattributes_SRCartwork_types’, true ) );
// Loop through the repeater fields and put their values into the array.
for ( $i=0; $i<$count; $i++ ) {
$selection = get_post_meta( $related_source_id, ‘SRCattributes_SRCartwork_types’.’_’.$i.’_’.’SRCartwork_type’ );
$inheritedvalues[$i] = $selection[0];
}
// Add the selection array to the field choices of the active Admin screen Artwork post
$field[‘choices’] = $inheritedvalues;
//Return the new selection options
return $field;
});
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.