Choice fields
5 min read
Choice fields let an editor pick from a fixed list. They share one Choices editor and the same rule: the value is stored, the label is shown.
Type | Type id | Picks | Column |
|
|---|---|---|---|---|
Select |
| One |
| the stored value (string) or |
Radio buttons |
| One |
| the stored value (string) or |
Button group |
| One |
| the stored value (string) or |
Checkboxes |
| Several |
| array of stored values, or |
True / false |
| On or off |
|
|
Type settings of Select, Radio buttons, Checkboxes and Button group
Setting | Default | Effect |
|---|---|---|
Choices | none | The list of options. Each choice has a Value (stored) and a Label. |
Output | Label | What |
Layout (Radio buttons and Checkboxes only) | Vertical | Vertical or Horizontal list on the edit screen. |
Allow no choice (Button group only) | Off | Adds a None button so editors can clear the value. |
The Choices editor
- Click + Add choice for each option, or Paste list and enter one choice per line as
value : Label(for examplered : Red), then Apply list. Edit as rows switches back. - Use short, stable values (
red,in_stock) and friendly labels ("Red", "In stock"). If you leave a label empty it becomes the value. - A value is trimmed, limited to 191 characters and must be unique; empty or duplicate values are dropped. A field can have up to 500 choices.
- Changing a label never touches stored data. Changing or deleting a value does not update saved posts: they keep the old value. It still prints (as the raw value, because no label matches), but the control shows nothing selected and saving the post clears it.
Select
An editor sees a drop-down. The first option is — Select — (empty), hidden once a required field has a value. A value that is not in the list is refused and the field is saved empty.
$color = wpmcf_get_field( 'product', 'color' ); // "blue" (the stored value, not "Blue")[wpmcf_field group="product" field="color"] → Blue (Output = Label)
[wpmcf_field group="product" field="color" wrap="false"] → BlueIf Output is set to Value, the same shortcode prints blue.
To get the label in PHP, look it up in the field's choices, or print with wpmcf_the_field():
wpmcf_the_field( 'product', 'color', null, [ 'wrap' => false ] ); // BlueRadio buttons
The same as Select, shown as a list of radio buttons. There is no empty option, so once a choice is saved an editor cannot return to "nothing" in the browser (use Button group with Allow no choice, or Select). Layout controls whether the buttons stack or sit in a row.
Button group
Looks like a row of connected buttons; behaves like Radio buttons. Turn on Allow no choice to add a None button, which clears the value. The None button is not shown for a required field.
Checkboxes
Several choices can be ticked. The value is a JSON list stored in one column.
$tags = wpmcf_get_field( 'product', 'features' );
// ["wifi", "usb_c"] (array of stored values, in the order they were submitted; the edit screen submits them in Choices order)
// null when nothing is ticked- Unknown values and duplicates are dropped when saving. Saving with every box unticked clears the value.
- Printed output is the labels joined by commas:
Wi-Fi, USB-C. - Passing a single string to
wpmcf_update_field()(instead of an array) saves nothing: the field is cleared. Always pass an array.
wpmcf_update_field( 'product', 'features', [ 'wifi', 'usb_c' ], $post_id );
foreach ( (array) wpmcf_get_field( 'product', 'features' ) as $value ) {
echo '<li>' . esc_html( $value ) . '</li>';
}Checkboxes cannot be indexed (the column is JSON) and cannot be used as a Programmatic SEO data source column.
True / false
A single on/off switch.
Setting | Default | Effect |
|---|---|---|
Label when on | Yes | Text shown next to the switch on the edit screen, and printed when the value is on. |
Label when off | No | Printed when the value is off. |
How a value is saved: 1, true, yes, y and on (any letter case) become on; anything else, including 0, false, no, off, an empty string or maybe, becomes off. Passing null clears the field. An unticked switch always posts "off", so an unticked field is stored as 0, not empty.
| Meaning |
|---|---|
| on |
| off |
| no value has been saved |
if ( wpmcf_get_field( 'product', 'featured' ) ) {
echo '<span class="badge">Featured</span>';
}[wpmcf_field group="product" field="featured"] → Yes (or your "Label when on")Things to know:
wpmcf_has_field()istruefor an off value, becausefalseis a saved value. Test the value itself withif ( wpmcf_get_field(...) ).- With the other output formats (
html,attr,url,js) a true value prints1and a false value prints nothing. - Inside a Group or Repeater the value is stored as
1/0and is returned as the number1or0, not as a boolean. Use a loose check (if ( $row['featured'] )). - Column type
TINYINT(1); you can filter on it (WHERE featured = 1).
Conditional logic
True / false is the most common controlling field: show a field only when a switch is on, using "equals 1". Select, Radio buttons and Button group compare their stored value; Checkboxes match when one ticked value equals the rule's value. See Settings every field has.
Changing the type later
Select, Radio buttons and Button group convert into each other without a data backup, keeping the choices. Checkboxes (JSON) and True / false convert to fewer types; the builder asks you to confirm when data could be lost.
Troubleshooting
- The shortcode prints a value instead of a label. Output is set to Value, or the stored value no longer exists in the Choices list.
- A choice shows nothing selected on the edit screen. The stored value is not in the list any more (renamed value). Re-select a choice and save.
wpmcf_update_field()cleared my checkboxes. You passed a string or only unknown values. Pass an array of valid stored values.- My True / false shows "Yes" but I want "Open". Change Label when on and Label when off.
Still stuck? Contact support