Asking for help.
Is there any way to get all DISTINCT values of custom field for ALL posts?
Thanks.
I’m looking for this as well. I found a solution here: https://stackoverflow.com/a/20293486/1766219
Note, that there is typo on that solution, which I have corrected in below-written solution:
global $wpdb; // (this is required when are you inside the function)
$values = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->posts p WHERE meta_key = 'NAME_OF_THE_FIELD_YOURE_AFTER' and pm.post_id=p.ID and p.post_type='CUSTOM_POST_TYPE_NAME' ",ARRAY_A);
print_r($values);
Could you use something like the below:
$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 ) :
foreach ( $filtered_colours as $colour ) :
echo $colour;
endforeach;
endif;
Depends on how you need to show/use the values I guess