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