Support

Account

Home Forums General Issues Get stored field values from custom field Reply To: Get stored field values from custom field

  • 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.