tad CMB2 fields getting its first utility function
April 15, 2015
A utility addition to CMB2 additional fields.
Getting those values out
I've just begun using the Custom Meta Boxes 2 fields, the nestable list especially, and found the need to get the values out of the meta the list is saving and doing it in a way that's use oriented and not integrity oriented.
To be able to do that I've added the functions file to the repository that is adding, now, just the function below
/**
* Extracts the elements unique keys for a list level.
*
* @param $post_id The ID of the post to which the nestable list is a meta.
* @param $meta_key The meta key the nestable list is saved in
* @param string $key The name of the unique key used in the list, defaults to 'id'
* @param null $key_value The optional unique key value to grab deeper than root elements.
*
* @return array An array of unique key values.
*/
function tad_extract_keys_from_list( $post_id, $meta_key, $key = 'id', $key_value = null );
given a value saved by a nestable list in the format, once json_decode
d, below
array(
array(
'id' => 1,
'text' => 'One',
'children' => array(
array(
'id' => 2,
'text' => 'Two'
),
array(
'id' => 3,
'text' => 'Three'
)
)
),
array(
'id' => 4,
'text' => 'Four'
),
array(
'id' => 5,
'text' => 'Five',
'children' => array(
array(
'id' => 6,
'text' => 'Six'
),
array(
'id' => 7,
'text' => 'Seven'
)
)
)
);
a call to the function like this one
$post_id = 23;
$meta_key = 'the_list';
function tad_extract_keys_from_list( $post_id, $meta_key, 'id' );
would yield the [1,4,5]
array returning root level ids; the function can fetch deeper levels adding the fourth parameter
function tad_extract_keys_from_list( $post_id, $meta_key, 'id', 5 );
would yield the [6,7]
array.
While not ground-breaking still the missing part of the equation.