Custom field types: Select

GD Custom Posts And Taxonomies Tools Pro implements many custom fields types, and while most of them are straightforward, one of them can be a bit intimidating considering the different ways it could be set up. This complex custom field type is Select, and here is how to use it.

Custom Field Type: Select
Custom Field Type: Select

When you set the custom field with select type, you will see full field dialog like on the image on the right. Field Settings contains two options: Selection and Method. First one has 4 values, two of them allow multiple items selection (displayed as Select and Checkbox control) and single item selection (Select and Radio control). Method is used to define getting of values to populate control and user can select from.

Most important thing about select control is how to set values for it. There are 3 methods available for this.

Normal List

Just list the values you want displayed and that’s it. Each value goes in a new line, and you can add as many of them as you want. When saving, plugin will remove all empty lines.

Associative list

For more complex setups, you can use associative array of value. In this case, each line needs to contain key and value for it. To separate key and value use pipe ( | ) character. When saving this field values, if the key|value is not found, both key and value will be set the same.


In some cases, you would need to get dynamic data, and in that case, it is best to use a function that returns associative array of keys and values. To do so, plugin needs to know what functions are available. So, first, we need one function and hook that will let plugin know about function(s) we want to add and functions we are going to use. Here is the example of this:

[php]add_filter(‘gdcpt_custom_field_function’, ‘my_select_functions’);
function my_select_functions($list) {
$list[‘my_data_function’] = ‘My Data Function’;
$list[‘myClass::get_data’] = ‘My Data From Class’;
return $list;

So, this code adds filter and a function for the hook. This filter passes list of functions. Each function is added with the key for the array and a name to be used for display purposes. In this example, line 3 adds first function. Actual function name is ‘my_data_function’, with the title set to ‘My Data Function’. Line 4 adds function that is in the class. Class is ‘myClass’ and function in it is ‘get_data’. Again, title is ‘My Data From Class’. Key in the list is most important since it represents the actual function. And now, for this code to work, we need to add the function and class with a function.

[php]function my_data_function() {
return array(
‘F’ => ‘Female’,
‘M’ => ‘Male’

class myClass {
public static function get_data() {
return array(
‘one’ => ‘First Value’,
‘more’ => ‘One More Value’,
‘three’ => ‘Third Value’

Select Functions Example
Select Functions Example

If you now create new custom field that has, and you want to use Function for select method, you will see list of registered data functions. Two functions we added are listed. You can also see 3 more functions marked as Default. They are built into the plugin and you can always use them (you can enable them or disabled them from the plugins Settings panel, Metabox tab). These built in functions are added in version 3.6.1 (April 10, 2012).

You can add as many functions as you want, and they can be used to return all sorts of arrays to populate your select fields.


Leave a Reply