Support

Account

Home Forums ACF PRO load_field

Solved

load_field

  • Hi
    I am having a lot of trouble with how to use load_field. What I need to do is have post(s) in a custom CPT called Source where each has a repeater field called SRCartwork_type that can have multiple options added as each individual repeater fields value. Then in another custom CPT called Artwork (the current admin screen) that has a Relationship Field which has one of the posts from Source CPT selected (related) and to use its repeater field values for SRCartwork_type, from the related Source post, as the selection choices in a field called ARTartwork_type which is in the current (admin screen) Artwork CPT post (so that each Artwork post that is in the admin screen will use its proper related Source posts values from the SRCartwork_type repeater field.

    CPT Source
    POST ID 14342 (the current related Source post)
    REPEATER FIELD SRCartwork_type (these values will change for each Source post)
    SELECT FIELD ONE = Lithograph (selected)
    SELECT FIELD TWO = Etching (selected)

    CPT Artwork (related to the field above 14342)
    POST ID 14341 (this is the active Artwork post in the admin screen and will change depending on which Artwork post is being edited.)
    SELECT FIELD ARTartwork_type (field_5bf31d5e8e1eb)
    Option 1 = Lithograph (populated by load_field)
    Option 2 = Etching (populated by load_field)

    Basically, the selection options for an active admin screen post for a specific field are dynamic and will change for each edited post depenidng on the related posts values for a repeater field.

    // 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;

    });

    I can not hardcode the Field Key or the Post ID because it needs to work for every Artwork Post that is currently in the admin screen and has a relationship to a Source post. Additionally, this is not limited to the ARTartwork_type field but would be used for dozens of other fields that need their selection options loaded from other values in the related Source post.

    Thank you in advance for your time and help!

  • 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;
    });
  • I also tried this…

    $post_id = $_GET['post'];
    $related_ids = get_field( 'ARTrelationship_SRCrelationship', $post_id ); //Will not work (it removes the default selection and replaces them with 'No results found')

    instead of using ‘false’ and it will not work either. The only thing that works is to hardcode the ID. This is not dynamic and will not work based on the current admin screens active Post ID.

  • Hi again

    I also tried this…

    global $post_id;
    $post_id = $_GET['post'];
    
    add_filter('acf/load_field/key=field_5bf31d5e8e1eb', function ( $field ) use ($post_id) {
    
    $related_ids = get_field( 'ARTrelationship_SRCrelationship', $post_id ); //Will not work (it removes the default selection and replaces them with 'No results found')
    
    // this still works
    $related_ids = get_field( 'ARTrelationship_SRCrelationship', 14341 ); //Works and adds 'Lithograph' and 'Etching'
  • I had this same problem earlier in the code when using have_rows() to retrieve the values from the related repeater field (to populate the ARTartwork_type selection set instead of get_post_meta().

    Where if I used the ID as a variable ($related_source_id) with have_rows() it would NOT work…

    if( have_rows('SRCattributes', $related_source_id) ) { 
    		
    	while( have_rows('SRCattributes', $related_source_id) ) {
    
    		the_row();
    
    		if( have_rows('SRCartwork_types') ) { 
    
    			$artwork_type_count = count(get_field('SRCattributes_SRCartwork_types', $related_source_id));
    
    			while( have_rows('SRCartwork_types') ) { 
    
    				the_row();
    
    				$inheritedvalues[] = get_sub_field( 'SRCartwork_type' );
    
    			}
    					
    		}
    
    	}
    		
    }

    but if I switched to hardcoding the $related_source_id to (14342) it worked…

    if( have_rows('SRCattributes', 14342) ) { 
    		
    	while( have_rows('SRCattributes', 14342) ) {
    
    		the_row();
    
    		if( have_rows('SRCartwork_types') ) { 
    
    			$artwork_type_count = count(get_field('SRCattributes_SRCartwork_types', $related_source_id));
    
    			while( have_rows('SRCartwork_types') ) { 
    
    				the_row();
    
    				$inheritedvalues[] = get_sub_field( 'SRCartwork_type' );
    
    			}
    					
    		}
    
    	}
    		
    }

    However when I switched to using get_post_meta() it worked with a variable for the $related_source_id…

    $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];
    		
    	}

    So I got this portion of the code to work when I used get_post_meta() method (something about an infinite loop.)

    I am hoping there is a similar fix for getting the $post_id of the active Admin screen to work where I can use a variable for post ID of the current admin screen rather then having to hardcode it, which does not work since it needs to be dynamic.

    I really hope this all makes sense! thank you so much if you can help (Its driving me crazy).

  • This was a really tricky one and thanks to Patrick’s help from ACF we figured it out. It had nothing to do with the Post ID and the method of getting the ID but rather the fact that the select field had been checked for “Use AJAX to lazy load choices?” and apparently when this is active you can’t get the ID to use with load_field but rather have to hardcode it. So we turned it off and all is fine now.

    Thanks again

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.