One of the most important features of Meta Box is field cloning. It allows you to create unlimited clones of fields' inputs. This feature is also called repeatable fields in other plugins. In Meta Box, it's built-in (free) and is supported in all fields, no matter that field is a simple input, a gallery (image_advanced) or a group.

Technically, when a field is cloneable, its value is an array. When saving the value in the database, Meta Box transforms that value into a serialized string and saves it in a single row in the meta table. This method has an advantage: you use only one row to store many values, which reduce the database size. If you have some nested groups of fields, which can contain up to 20 sub-fields, then you still need only one row in the database.

While it sounds good at first, it has a disadvantage: you couldn't query posts by cloneable fields' values. For example, if you have a custom field start_date for the event custom post type, then you can query events in May 2019 like this:

$args = [
    'post_type'      => 'event',
    'meta_query'     => [[
        'key'     => 'start_date',
        'value'   => ['2019-05-01', '2019-05-31']
        'compare' => 'BETWEEN',
    ]],
];
$query = new WP_Query( $args );

If start_date is cloneable, then its value looks like this: a:2:{i:0;s:10:"2019-05-01";i:1;s:10:"2019-04-30";}(FYI: this is the serialized string of ['2019-05-01', '2019-04-30']). And WordPress can't perform the compare operation above because it's incorrect format for a date.

But that's not the case anymore. Since version 4.14.7, Meta Box introduces a new feature that allows you to save cloneable values in multiple rows in the database, each row contains one value. That means if start_date is cloneable and has 2 values ['2019-05-01', '2019-04-30'], it will be saved in 2 rows in the database, one for 2019-05-01 and one for 2019-04-30. Because the value is not serialized, WordPress can perform the compare operation above. In the result, you can query by cloneable fields' values!

To enable this feature, you need to add an option to the field's settings array:

'clone' => true,
'clone_as_multiple' => true, // THIS
// Other settings go here

This option is disabled by default, to keep backward compatibility with your existing fields. Your data won't be lost when you update to the new version.

So you now can make your choice on how to save cloneable values in the database. If you don't need to query the data, using serialized arrays is the best option. Otherwise, set clone_as_multiple to true for your fields and you're ready to query.

We hope this feature will give you more flexibility while using Meta Box. If you haven't updated the plugin, please update now. And let us know what you think in the comments!

11 thoughts on “Introducing "Clone as Multiple" Feature

    1. It works for the top-level groups only. For sub-fields, including nested fields, the data is still saved as serialized array.

    1. It works with top-level cloneable groups. For sub-fields, the values are still saved as serialized arrays.

  1. A good enhancement! Would you introduce a way to unserialise the old (saved as serialised) data and put that into different database row fields?

    Or if someone realise that he don't need multiple clone later, any option to serialise the (saved unserialised) data and put into one database row field?

    Thanks

    1. Yes, they are. There's no difference between groups created with code and with the Builder.

Leave a Reply

Your email address will not be published. Required fields are marked *