Php – WordPress: Get all values of a custom field

custom-fieldsPHPWordpress

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.

Thank you in advance!

Best Answer

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.

Related Topic