Usually, Custom Fields are single values. Not so Checkboxes. Those are arrays.
Below Code lets you var_dump
and get_post_meta()
each stored value
This is the vardump for the whole Checboxes Field:<br> <?php $checkboxes = get_post_meta($post->ID, 'checkboxes'); var_dump($checkboxes); ?><br> This is the above's array vardump<br> <?php foreach ($checkboxes as $checkbox => $value) { var_dump($value); foreach ($value as $saved) { ?><br>This is the single saved value<br><?php var_dump($saved); foreach ($saved as $bare_value) { ?><br>This is the single echoed value<br><?php var_dump($bare_value); } } }
This is the same for Generic CRED Checkboxes and Types Checkboxes.
Above code vardumps the values, of course you can echo them, or do whatever else you like with it.
It might come in handy when you need to get values from one checkboxes field and update another field with this values.
Mind you, in a ShortCode to return the above, you must return the values, not echo, and since it can have many values, you need to append the string to the returned value.
Also you need to move the Return statement out of the Foreach’s.
This below example will output all saved values as a shortcode (useful for generic Fields)
function saved_checkboxes_values( $atts ){ global $post; $checkboxes = get_post_meta($post->ID, 'checkboxes'); $values = ''; foreach ($checkboxes as $checkbox => $value) { foreach ($value as $saved) { foreach ($saved as $bare_value) { $values .= '<li>'.$bare_value.'</li>'; } } } return $values; } add_shortcode( 'saved-checkboxes-values', 'saved_checkboxes_values' );
Then you can use it as this:
[saved-checkboxes-values]