WPLake > Learning Hub > ACF Select Field

ACF Select Field

Discover how the ACF Select field stores values, supports dynamic choices, and offers versatile formats for flexible data manipulation.

Key Points at a Glance

  1. Storage Location: The Select field stores values in the post meta table, allowing for easy updating of labels without modifying existing data.
  2. Dynamic Choices: Choices for the Select field can be defined dynamically using the 'load_field' filter, enabling conditional selection options.
  3. Value Formats: The get_field function returns six different value formats depending on field settings, offering flexibility for data manipulation.

Table of Contents

About the ACF Select field

Advanced Custom Fields (ACF) is one of the prominent meta field plugins. The Select field is categorized under the Choice group fields, enabling editors to make selections from a predefined list.

Note: Keep in mind, that unlike select fields in other meta plugins, such as Pods, the ACF Select field is specifically tailored for choosing plain text options only.

If you require a selection of objects, such as posts, users, or taxonomies, it's advisable to consider using ACF Post Object, User, or Taxonomy fields instead. These fields are designed for handling selections of objects within WordPress.

ACF Select Field
The selection choices defined in the settings
ACF Select field look for editors
Editor's appearance of the ACF select field

1. Database value format of the Select field

Regardless of the Return format and multiple option state, the field only stores field values in the post meta table. This provides the flexibility to update labels later without needing to modify existing data.

ACF Select field value in the DB
In the database, only the field value is saved

If the multiple option is disabled, the field value will be a string containing the chosen value. Otherwise, it will be a serialized array of chosen values. Even if a single item is chosen, the array format will be used when the multiple setting is enabled.

It's important to note that since the select field stores data in the post meta table, it is not suitable for categorizing items. Unlike WordPress taxonomy tables, the Postmeta table is not optimized for search queries.

Therefore, if you use the select field to categorize a large number of CPT or post items, you may encounter performance issues.

In such cases, it is recommended to use the WordPress taxonomies or the ACF Taxonomy field with settings to store values as traditional taxonomies. This will ensure better performance and scalability for categorizing your items.

2. How to set and get ACF Select choices dynamically

2.1) Dynamically defining select options

As mentioned earlier, the ACF select field supports only text choices. By default, they're defined statically in the field settings. This fits in most cases, but sometimes you may need to define them dynamically.

Fortunately, ACF developers took care of this and provided a filter that allows populating items dynamically. Let's consider a simple example. Suppose we have an options page with a boolean setting that enables or disables detailed product categories.

If it's enabled, the select field should display the full list of options. If it's disabled, only the general option should be available. Below, you can see the code that accomplishes this:

<?php

add_filter(
	'acf/load_field/name=product_category',
	function ( $field ) {
		// Check if the detailed product category field is available for selection.
		$is_category_available = get_field( 'enable_detailed_product_category', 'option' );

		// Define choices based on whether product categories are available.
		$choices = true === $is_category_available ?
			array(
				'general'     => 'General',
				'electronics' => 'Electronics',
				'clothing'    => 'Clothing',
				'home'        => 'Home & Garden',
				'sports'      => 'Sports & Outdoors',
			) :
			array(
				'general' => 'General',
			);

		// Assign choices to the field.
		$field['choices'] = $choices;

		return $field;
	}
);

Note: The acf/load_field filter enables dynamic population of select field choices based on global or already saved field values. It's triggered only once during page loading, so it can't be used to load choices dynamically based on the current state of other fields. In such cases, consider using conditional rules.

2.2) Dynamically obtaining select options

The get_field response includes only the information of the selected items. While this is suitable in most cases, sometimes we need to retrieve all available options in our template. Manually inserting the available options into the code isn't advisable, as it won't reflect later changes to the list.

Fortunately, ACF allows us to dynamically get select field options. For this purpose, we should use the get_field_object function. It accepts the field name or key. We recommend always passing the key, as you may have several fields with the same name in different groups.

<?php

// todo put your field key here
$field = get_field_object('field_abc123456789');
$choices = $field['choices']??[];

While the group UI displays only the field name, the field key is included in the group UI markup, so we can retrieve it from there.

Click on the target field item in the Group interface, and find the parent div with the acf-field-object class. This div contains the data-key property with the field key, for example, field_abc123456789.

Tip: You can quickly retrieve and display a list of available options using templates from the Advanced Views plugin. The plugin introduces smart templates with automated template generation and help to avoid repetitive tasks. For example, the select fields there already have the 'choices' property, which contains the options list, saving us from extra actions.

3. Return value formats of the Select field

While only the field value is stored in the database, the behavior of the get_field function for the Select field in code may vary depending on the field settings. The response could include:

  • An array of values
  • An array of labels
  • An array of info items containing both value and label
  • Value
  • Label
  • An array containing both value and label

The 'multiple' setting determines whether the return value will be an array of items or a single item, while the 'Return format' setting determines the value type. If the 'multiple' setting is enabled, the function will always return an array, irrespective of whether the single item option is checked.

Although the extensive field settings offer flexibility to tailor setups according to our requirements, they can also introduce complexity to coding tasks as we must consistently remember the specific field settings.

To address this challenge, consider leveraging smart templates from the Advanced Views plugin.

These templates introduce automated generation and include built-in post queries, simplifying tasks such as retrieving field values, converting formats, and passing them to the template. This allows you to focus on designing the layout without worrying about the intricacies of field settings.

Summary

The ACF select field is intended for creating choice inputs based on a pre-defined set of text options. Unlike other meta plugins, the ACF select field is exclusively designed for text options.

If you require a selection of objects, such as posts, users, or taxonomies, it's advisable to consider using ACF Post Object, User, or Taxonomy fields instead. These fields are designed for handling selections of objects within WordPress.

The list of options is initially defined statically in the field settings, but can also be defined dynamically using the 'load_field' filter.

The select field stores only value in the database, providing flexibility to update labels later without needing to modify existing data. To retrieve the list of pre-defined choices in our templates, we can use the get_field_object function, which saves us from making duplicates.

Alternatively, we can utilize smart templates from the Advanced Views plugin, which includes the 'choices' property for the select field out-of-the-box.

The get_field function may return six different value formats for the select field depending on the multiple setting and return format.

While it offers flexibility, working with it in code may be difficult. Consider using smart templates from the Advanced Views plugin to avoid the necessity of remembering and handling the formats.

Frequently Asked Questions

  1. Can the ACF Select field store values other than text?

    No, the Select field in ACF is specifically designed for text options only. For selections of objects like posts, users, or taxonomies, it's recommended to use ACF Post Object, User, or Taxonomy fields instead.

  2. Can the Select field be used for categorizing a large number of items in WordPress?

    While possible, using the Select field for categorization of large numbers of items may lead to performance issues. It's recommended to use WordPress taxonomies or ACF Taxonomy fields for better performance and scalability.

  3. Is it necessary to manually handle the different value formats returned by the get_field function for the Select field?

    While the get_field function returns different value formats depending on field settings, utilizing smart templates from the Advanced Views plugin can simplify coding tasks by automating format handling and reducing the need for manual intervention.

Content links (28)

Related articles

About the Author

Maxim Akimov

Certified WordPress expert from Ukraine with over 8 years of experience. Advocate of the BEM methodology and the overall modular approach. Loves sporting activities and enjoys going to the gym and regularly plays table tennis.

0 Comments

    Leave a comment

    Reply to 

    Please be considerate when leaving a comment.

    Not shown publicly

    Got it