Misc / Storing Data Unserialized

Read More: API Docs

Overview

By default, grouped or repeating fields store their data as serialized PHP in a single meta entry. This method favors data retrieval, but has two main drawbacks: (1) it couples data structure and presentation, and (2) it’s difficult to query against (e.g. using a meta query). Therefore, there may be cases where you don’t want this behavior, and you want fields to be stored using their own meta keys.

Fieldmanager fields have the option $serialize_data to specify if that element should store its data serialized (the default) or unserialized (that is, using individual meta keys). This can be applied to a single repeating field, where the field is stored with duplicate keys, or to a group where each child gets its own key. In the case of a group, the field is prefixed with the group’s name and an underscore. Fieldmanager groups also have another option $add_to_prefix allowing you the choice of whether or not to add that group’s name to the prefix (true by default), which means you can completely separate your data structure from its presentation.

Groups can be nested any number of levels deep, and Fieldmanager will walk the nest until it finds either a group with $serialize_data = true or it finds a field. In other words, you can have a field nested within three groups, and still that field can have its own key. Again, you can choose individually which groups’ names will be prepended to the meta key. You can also mix-and-match depths, which is to say that you can have group with four children: three fields and another group, where the subgroup has two fields. You can set it such that the three top-level fields get their own keys, and the subgroup gets its own key with its children serialized, or you can set it such the subgroup’s fields each have their own keys too (making it so that all five fields have their own keys). While this may seem like a ridiculous edge case, it’s entirely likely that you would want a group to work this if it has a tabbed group within it (see example 5 below).

The main area where this doesn’t work is where a field’s ancestor has a limit != 1. The only way to support such a structure would be to save to meta keys like group_0_field, group_1_field, etc., which would not be sustainable and it would produce data that would be extremely difficult to work with in code.

Lastly, it’s worth noting that the submenu context does not support this at this time.

  1. Example 1: Repeating field where each iteration has its own key

    $fm = new Fieldmanager_Textfield( array(
    	'name'           => 'repeating_text_field',
    	'limit'          => 0,
    	'serialize_data' => false,
    ) );
    $fm->add_meta_box( 'Repeating Standalone Text Field', 'post' );
  2. Example 2: Groups where each child field has its own key (key name is {group}_{field})

    $fm = new Fieldmanager_Group( array(
    	'name'           => 'meta_fields',
    	'serialize_data' => false,
    	'children'       => array(
    		'text'     => new Fieldmanager_Textfield( 'Text Field' ),
    		'textarea' => new Fieldmanager_TextArea( 'TextArea' ),
    		'checkbox' => new Fieldmanager_Checkbox( 'Checkbox' ),
    	)
    ) );
    $fm->add_meta_box( 'Single-Level Group', 'post' );
  3. Example 3: Groups where each child field has its own key without the group prefix

    $fm = new Fieldmanager_Group( array(
    	'name'           => 'meta_fields',
    	'serialize_data' => false,
    	'add_to_prefix'  => false,
    	'children'       => array(
    		'text'     => new Fieldmanager_Textfield( 'Text Field' ),
    		'textarea' => new Fieldmanager_TextArea( 'TextArea' ),
    		'checkbox' => new Fieldmanager_Checkbox( 'Checkbox' ),
    	)
    ) );
    $fm->add_meta_box( 'Single-Level Group', 'post' );
  4. Example 4: Tabbed group where the fields have their own keys

    $fm = new Fieldmanager_Group( array(
    	'name'           => 'tabbed_meta_fields',
    	'tabbed'         => true,
    	'serialize_data' => false,
    	'add_to_prefix'  => false,
    	'children'       => array(
    		'tab-1' => new Fieldmanager_Group( array(
    			'label'          => 'Tab One',
    			'serialize_data' => false,
    			'add_to_prefix'  => false,
    			'children'       => array(
    				'text' => new Fieldmanager_Textfield( 'Text Field' ),
    			)
    		) ),
    		'tab-2' => new Fieldmanager_Group( array(
    			'label'          => 'Tab Two',
    			'serialize_data' => false,
    			'add_to_prefix'  => false,
    			'children'       => array(
    				'textarea' => new Fieldmanager_TextArea( 'TextArea' ),
    				'media'    => new Fieldmanager_Media( 'Media File' ),
    			)
    		) ),
    	)
    ) );
    $fm->add_meta_box( 'Tabbed Group', 'post' );
  5. Example 5: Tabbed group within a group, where all fields have their own keys

    This will save data to four meta keys: title, text, textarea, and media. All of the groups are ignored except in presentation.

    $fm = new Fieldmanager_Group( array(
    	'name'           => 'base_group',
    	'serialize_data' => false,
    	'add_to_prefix'  => false,
    	'children'       => array(
    		'title' => new Fieldmanager_TextField( 'Title' ),
    		'tabs' => new Fieldmanager_Group( array(
    			'tabbed'         => true,
    			'serialize_data' => false,
    			'add_to_prefix'  => false,
    			'children'       => array(
    				'tab-1' => new Fieldmanager_Group( array(
    					'label'          => 'Tab One',
    					'serialize_data' => false,
    					'add_to_prefix'  => false,
    					'children'       => array(
    						'text' => new Fieldmanager_Textfield( 'Text Field' ),
    					)
    				) ),
    				'tab-2' => new Fieldmanager_Group( array(
    					'label'          => 'Tab Two',
    					'serialize_data' => false,
    					'add_to_prefix'  => false,
    					'children'       => array(
    						'textarea' => new Fieldmanager_TextArea( 'TextArea' ),
    						'media'    => new Fieldmanager_Media( 'Media File' ),
    					)
    				) ),
    			)
    		) )
    	)
    ) );
    $fm->add_meta_box( 'Tabbed Subgroup', 'post' );
  6. Example 6: A group with a child field and a subgroup, each with their own keys, where the subgroup does use serialized data

    Here, the stored meta keys will be 'meta_fields_text' and 'meta_fields_subgroup'.

    $fm = new Fieldmanager_Group( array(
    	'name'           => 'meta_fields',
    	'serialize_data' => false,
    	'children'       => array(
    		'text'     => new Fieldmanager_Textfield( 'Text Field' ),
    		'subgroup' => new Fieldmanager_Group( array(
    			'children'       => array(
    				'text'     => new Fieldmanager_Textfield( 'Text Field' ),
    				'textarea' => new Fieldmanager_TextArea( 'TextArea' ),
    				'checkbox' => new Fieldmanager_Checkbox( 'Checkbox' ),
    			),
    		) ),
    	),
    ) );
    $fm->add_meta_box( 'Multi-Level Groups', 'post' );