Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Thank you in advance!

like image 988
Imaginary Avatar asked Feb 14 '12 17:02

Imaginary


People also ask

How do I get custom field values in WordPress?

To add a Custom Field, type in the Key (labeled “Name”) and Value, then click Add Custom Field. After it's added, you can delete or update it from buttons below the Key/Name: After you have used Custom Fields, the keys will form into a dropdown menu for easier selection.

Which function is used to retrieve value of a custom field?

get_post_custom_values( string $key = '', int $post_id ): array|null. Retrieve values for a custom post field.

How do I export a custom field in WordPress?

To export WordPress custom fields to CSV/XML go to WP All Export › New Export and select the post type you'd like to export. WP All Export will automatically detect the custom fields from your posts. Drag and drop to select fields to export, arrange the spreadsheet columns, and more.


1 Answers

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.

like image 70
decomush Avatar answered Sep 30 '22 19:09

decomush