WordPress: Get all values of a custom field

I’m using Verve Meta Boxes. I want to make a menu out of one of the custom fields. How can I return all of the custom field values? For example, if I had a custom select field called “fruit” and as options I have “apples”, “oranges”, and “bananas”, how could I get a complete list of those values, as an array perhaps? I can get the ones associated with a post:

get_post_custom_values('fruit')

…but I can’t work out how to get the whole list.

Read More

Thank you in advance!

Related posts

Leave a Reply

4 comments

  1. In case someone still wondering:

    global $wpdb;
    $results = $wpdb->get_results( 'SELECT DISTINCT meta_value FROM wp_postmeta WHERE meta_key LIKE "FIELD_NAME"', OBJECT );
    

    Just make sure your postmeta table is “wp_postmeta” (default) and change FIELD_NAME with the name you created for the field in the admin.

  2. Try this one out:

    $fruits = trim(get_post_meta($post->ID,'fruits',true)); 
    $fruits_array = explode(',',$fruits);
    foreach($fruits_array as $f){
       echo $f.'<br/>';
    }
    

    Basically you need to separate your fruits name with comma in your custom field so that you will be able to explode them into array and echo the values one by one.

    Thanks,Dave

  3. I wasn’t able to find an elegant solution. What I ended up doing was loop through all of the posts and kept a record of unique values as I can across them, creating an array. Then I used that array to make my navigation.