0

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?

1 Answer 1

1

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'
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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