Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

We use the Types plugin to setup different custom products for a client. A certain custom post (product) contains some custom fields which is a drop-down select. When creating a new product of this type, the user may select one option from that list.

For example, let say that certain product has a custom field of type select with the following options set :

1 = Option A
2 = Option B
3 = Option C

Now, in a template, I'm trying to display the label for that custom field. The best thing I came up with is by doing :

$value = reset(get_post_custom_values('custom_field', $post->ID));

which would return a string, for example "1", the value of the custom field. Now, this is worthless as I need to display the label (the chosen item from the list, for example "Option A") in that template.

How do I find the associated label for that custom field value?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Just so you know, and after much code sniffing, I finally have a working solution :

First, the custom field may start with wpcf- (for example: wpcf-custom-field). This prefix may be ignored and only the remaining characters kept. As in

$customField = substr($fieldName, 5);  // $fieldName = 'wpcf-custom-field';

Now, the custom field data may be fetch via the function wpcf_admin_fields_get_field which is located in the Types plugin directory under includes/fields.php. For example :

$fieldConfig = wpcf_admin_fields_get_field($customField);

Among other things, the options may be collected with this code :

$fieldOptions = array();
if (isset($fieldConfig['data']['options'])) {
   foreach ($fieldConfig['data']['options'] as $option) {
      $fieldOptions[$option['title']] = $option['value'];
   }
}

Which, if the custom field is a type select, will return an array like

array(
   '1' => 'Option A',
   '2' => 'Option B',
   '3' => 'Option C'
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.