CMB2 Meta Splitting

Splitting the meta to allow easy querying while using CMB2 and prevent the silos effect.

The problem

I’ve used Custom Meta Boxes 2 in some projects now and one thing I find amazing is how easy it makes the creation of repeatable fields like this one
A CMB2 repeatable field
or field groups
A CMB2 group field
Sadly that repeatable data will be stored in the database in a serialized array that WordPress will handle nicely but will not allow for any kind of easy querying using a Meta Query; and I use meta queries quite often.

A partial solution

In the rush of a quick solution I’ve created a plugin that will take care, now just for posts, to create other non serialized meta data when saving a post meta fields.
This means that a field like the one above will end up in the database like this
Repeatable field in db
and a group with nested fields will end up like this
Group field in db
While not incredible it will allow queries like this one previously not possible

$posts = get_posts(array(
    'meta_key' => 'yourprefix_demo_text_split',
    'meta_value' => array('foo','baz', 'bar'),
    'meta_compare' => 'IN'
));

All the while preserving the order the meta values are stored in.

I appreciate your input