Skip to content

Select Field

Overview

The select field type allows users to choose one or multiple options from a predefined list. It supports grouped options, AJAX-based searches, sorting, and multiple selections. This field type is useful for selecting posts, categories, pages, or other custom-defined values.

Field Configuration

A simple implementation of the select field type:

php
array(
    'id'            => 'select_key',
    'type'          => 'select',
    'title'         => 'Select',
    'placeholder'   => 'Select an option',
    'options'       => array(
        'option-1' => 'Option 1',
        'option-2' => 'Option 2',
        'option-3' => 'Option 3',
    ),
    'default'       => 'option-1',
),

Specific Parameters

ParameterTypeDefault ValueDescription
optionsarray-Defines selectable options as key-value pairs.
defaultmixed-Sets the default selected option(s).
placeholderstring-Display text when no option is selected.
multiplebooleanfalseEnables multiple selections.
chosenbooleanfalseEnhances the select field with search functionality.
ajaxbooleanfalseEnables AJAX search for dynamically loading options.
sortablebooleanfalseAllows reordering selected options.
query_argsarrayarray()Defines parameters for querying dynamic content.
settingsarrayarray()Defines parameters for chosen setting.

Available Options (options)

The options parameter supports predefined values or dynamically loaded content. Here are some available options:

  • 'pages' - Lists available pages.
  • 'posts' - Lists posts.
  • 'categories' - Lists categories.
  • 'tags' - Lists tags.
  • 'menus' - Lists navigation menus.
  • 'users' - Lists users.
  • 'sidebars' - Lists registered sidebars.
  • 'roles' - Lists user roles.
  • 'post_types' - Lists registered post types.

Query Arguments (query_args)

When using AJAX-based selections, query_args can be used to filter the queried content. Supported arguments:

ArgumentDescription
post_typeDefines the post type to query.
taxonomyFilters by taxonomy.
posts_per_pageLimits the number of results.
max_postSets the maximum number of posts to retrieve.
orderbyDefines sorting order (e.g., post_title).
orderSets sorting direction (ASC or DESC).

Settings Arguments (settings)

ArgumentsTypeDefaultDescription
loadingTextstringLoading...Show while choices are being populated via AJAX.
noResultsTextstringNo results foundShow when a user's search has returned no results.
noChoicesTextstringNo choices to choose fromShow when a user has selected all possible choices, or no choices exist.
itemSelectTextstringPress to selectShow when a user hovers over a selectable choice.

General Parameters

ParameterTypeDefaultDescription
idstring-Unique identifier for the field.
typestring-Defines the field type.
titlestring-The title displayed for the field.
subtitlestring-The text displayed under the title.
classstring-Field additional class.
data_typestringserializeDefines how data is stored (e.g., serialize).
namestring-Custom name for the field.
placeholderstringNot selectedPlaceholder text for the input.
attributesarrayarray()Custom HTML attributes.
beforestring-Content to display before the field.
afterstring-Content to display after the field.
descriptionstring-A detailed description of the field.
descstring-A short description, used if description is not set.
helpstring-Additional helper text for guidance. Usually show on the top right corner of the field.
errorstring-Error message to display when validation fails.
dependenciesarray-Show/Hide a field base on another field value.

Example Usage

Grouped Options

php
array(
    'id'          => 'select_2',
    'type'        => 'select',
    'title'       => 'Select Group',
    'placeholder' => 'Select an option',
    'options'     => array(
        'Group 1' => array(
            'option-1' => 'Option 1',
            'option-2' => 'Option 2',
            'option-3' => 'Option 3',
        ),
        'Group 2' => array(
            'option-4' => 'Option 4',
            'option-5' => 'Option 5',
            'option-6' => 'Option 6',
        ),
    ),
    'default' => array('option-1'),
),

Multiple Options

php
array(
    'id'          => 'chose_select_key',
    'type'        => 'select',
    'title'       => 'Select',
    'chosen'      => true,
    'multiple'    => true,
    'placeholder' => 'Select an option',
    'options'     => array(
        'option-1' => 'Option 1',
        'option-2' => 'Option 2',
        'option-3' => 'Option 3',
    ),
    'default'     => array('option-1', 'option-2'),
),

AJAX Search for Posts

php
array(
    'id'          => 'select_6',
    'type'        => 'select',
    'title'       => 'Select with posts',
    'placeholder' => 'Select posts',
    'chosen'      => true,
    'ajax'        => true,
    'multiple'    => true,
    'options'     => array('posts'),
),

Custom Post Type Query

php
array(
    'id'          => 'select_8',
    'type'        => 'select',
    'title'       => 'Select Custom Post Type',
    'placeholder' => 'Select a post',
    'chosen'      => true,
    'ajax'        => true,
    'options'     => array('posts'),
    'query_args'  => array(
        'post_type' => 'my_custom_post_type',
    ),
),

Notes

  • multiple allows users to select more than one option.
  • chosen enhances the field with a searchable dropdown.
  • ajax should be used when dealing with large datasets to improve performance.
  • query_args helps filter specific content dynamically.
  • sortable is useful for managing the order of selected items.

This documentation ensures clear guidance on using the select field type with different configurations.