Support

Account

Home Forums Backend Issues (wp-admin) Dynamically populate select field from one group into another?

Solving

Dynamically populate select field from one group into another?

  • Hi there,
    So, I had found this snippet here:

    function acf_load_color_field_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
        
        
        // get the textarea value from options page without any formatting
        $choices = get_field('my_select_values', 'option', false);
    
        
        // explode the value so that each line is a new array piece
        $choices = explode("\n", $choices);
    
        
        // remove any unwanted white space
        $choices = array_map('trim', $choices);
    
        
        // loop through array and add to field 'choices'
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
                
                $field['choices'][ $choice ] = $choice;
                
            }
            
        }
        
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=color', 'acf_load_color_field_choices');

    And was trying to make it work where I can dynamically populate the venue_select field on my productions custom post type, the venue_name from my venues custom post type.

    I modified the code like this:

    function acf_load_venue_choices( $field ) {
        
        // reset choices
        $field['choices'] = array();
        
        
        // get the textarea value from options page without any formatting
        $choices = get_field('venue_name', 'venue', false);
    
        
        // explode the value so that each line is a new array piece
        $choices = explode("\n", $choices);
    
        
        // remove any unwanted white space
        $choices = array_map('trim', $choices);
    
        
        // loop through array and add to field 'choices'
        if( is_array($choices) ) {
            
            foreach( $choices as $choice ) {
                
                $field['choices'][ $choice ] = $choice;
                
            }
            
        }
        
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=venue_select', 'acf_load_venue_choices', 'production');

    But it’s just not working. Suggestions?

    Note that the ideal in this case would actually be to be able to populate the select field with the custom post type item titles instead of having to create a separate ACF field for the venue name in addition to the post title.

  • Hi @brotsky_pixie,

    Hmm… reading through your code, I have noted that your are using this code to get a value from an options page $choices = get_field('venue_name', 'venue', false);. Change it to the following: $choices = get_field('venue_name', 'option', false);

    For more information on this, have a look at: http://www.advancedcustomfields.com/resources/how-to-get-values-from-an-options-page/

  • The reason I was doing that is that I have not assigned those values to the Option page. I have assigned them to the Venue page.

  • Maybe I need to explain a little more about what I’m trying to do:

    I run an opera company. I’ve created several custom post types in order to gather the necessary information to display everything necessary for a given show. These custom post types include: Venues, Creative Staff, Performers, and Production.

    Each of these custom post types are assigned a different group of custom fields.

    Within the Production page, I’m wanting to select from venues created in the venues custom post. Same with selecting staff members that have been created within the “Creative Staff” custom post type and the “Performers” custom post type.

    The reason for this is that venues may be different from production to production, but some staff members and performers will appear in multiple productions and I don’t want to enter their information over and over.

  • Hi @brotsky_pixie,

    Well, in that case, you have to specify a $post_id of the venue’s page that contains the ACF fields that you want to grab values from as explained in the docs 🙂 Here is the link: http://www.advancedcustomfields.com/resources/get_field/

  • I *thought* that’s what I was doing with the $choices = get_field('venue_name', 'venue', false); since that’s calling the “venue” $post_id. What am I missing?

  • Hi @brotsky_pixie

    Not really. the “venue” is not a post_id for the custom post. The post_id will be a numeric value. From your CPT post edit screen, the URL will be something like this: http://your-domain.com/wp-admin/post.php?post=2&action=edit. To get the post_id, look at the post=2 section of the URL. The post_id will be the number after post=. Like in the above example, the $post_id will be 2

  • Hi @brotsky_pixie

    Not really. the “venue” is not a post_id for the custom post. The post_id will be a numeric value. From your CPT post edit screen, the URL will be something like this: http://your-domain.com/wp-admin/post.php?post=2&action=edit. To get the post_id, look at the post=2 section of the URL. The post_id will be the number after post=. Like in the above example, the $post_id will be 2

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

You must be logged in to reply to this topic.