Ok, so probably the meta data is saved correctly, now we are going to retrieve it.
I’m not sure where and how you’re adding this code, it looks like a metabox?
The checkboxes aren’t magicly getting checked, you need to add some logic to it.
First we need to have a closer look how you save the checkbox data.
We want to save to the DB which checkboxes are checked.
This array:
$features = array();
$features[0] = "star";
$features[1] = "triangle";
$features[2] = "square";
Is not going to tell us which categories are checked.
We want to save the array like this:
$features = array();
$features['star'] = 1; // checked
$features['square'] = 0; // not checked
$features['triangle'] = 1; // checked
Above the checkbox HTML you need to get the features data (post_meta) from the DB.
Add this above the html:
<?php
global $post;
$features = get_post_meta( $post->ID, 'category', true );
?>
When you want to check a checkbox you need to add the attribute checked="checked"
to it.
We only want to do this if the features array from the DB is telling us to do so.
You can achieve this with this checkbox html:
<input type="checkbox" name="checkbox_name" id="checkbox_id" value="triangle" <?php echo (isset($features['triangle']) && $features['triangle']) ? 'checked="checked"' : '' ?> />
This:
<?php echo (isset($features['triangle']) && $features['triangle']) ? 'checked="checked"' : '' ?>
Checks if the array key (triangle) is present in the $features
array and if the value is set to 1 (or any positive value). If yes, it outputs checked="checked"
, if not, it does nothing.
Regards, Bjorn