Support

Account

Home Forums General Issues Get stored field values from custom field

Solving

Get stored field values from custom field

  • Hi!

    I would love to set up a custom field that gives the user the option to select values that have been saved into posts which use another field. Meaning: When I save 10 posts that have a custom color picker field I get 10 color values stored under this post type. How can I create a new custom field that shows in another post type from where the user can select one (or multiple) of those 10 color values?

    Thanks Sascha

  • Following this topic too.

  • I’m not sure if something like this could work:

    
    <?php
    ##########################################
    #  Populate ACF field with list of colours
    ##########################################
    function load_colours_into_acf_select_field( $field ) {
    
    	$args = array(
    		'numberposts'	=> -1,
    		'post_type'		=> array('post', 'page'),
    		'meta_key'		=> 'colours', // your existing colur select field
    	);
    
    	// query
    	$the_query = new WP_Query( $args );
    	if( $the_query->have_posts() ):
    		$colours = array();
    		while( $the_query->have_posts() ) : $the_query->the_post();
    		$colours[] = get_field('colours');
    		endwhile;
    	endif;
    
    	// remove duplicates
    	$filtered_colours = array_unique($colours);
    	
    	if ( $filtered_colours ) :
    		//Add empty option
    		$field['choices']["0"] = 'Select a colour';
    
    		foreach ( $filtered_colours as $colour ) :
    			$field['choices'] = $colour;
    		endforeach;
    	endif;
    
    	return $field;
    }
    
    //Change colour_dropdown to your field which is assigned to your custom post type
    add_filter( 'acf/load_field/name=colour_dropdown', 'load_colours_into_acf_select_field' );
    

    Hopefully, the code/comments will help. It’s not tested and simply cobbled together, however, it should be a good starting point.

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

You must be logged in to reply to this topic.