Splitting the meta to allow easy querying while using CMB2 and prevent the silos effect.
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
or field groups
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.
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
and a group with nested fields will end up like this
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.