Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Using the following code for the custom checkbox to save an array of values. When the [] is left off everything works, when it's added back to save the value as an array it does not work. Any ideas...?

$posts = get_posts("post_type=test&orderby=title&order=ASC&numberposts=1000");
foreach ($posts as $postv) {
$postname = $postv->post_title;
$postslug = $postv->post_name;

<input type="checkbox" name="' . $this->prefix . $customField['name'] . '[]"
value="' . $postslug . '" ';
$sponsorselected = get_post_meta($post->ID, 'eventsponsor', $single = true);
if ((!empty($sponsorselected))&&(in_array($postslug,$sponsorselected))
{echo 'checked '; } 
echo 'style="width: auto;border:0;" />

foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) && trim( $_POST[ 
$this->prefix . $customField['name'] ] ) ) {
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ 
$this->prefix . $customField['name'] ] );
} else {
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
share|improve this question

closed as too localized by toscho Mar 6 at 23:37

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

2 Answers

Thanks for your response, I like the more concise code, unfortunately it still does not work. It all looks correct when viewing the source...

<input type='checkbox'  name='eventsponsor[]' value='yes' style='width:
auto;border:0;' />

However nothing is saved to the database after clicking the update button. Any additional guidance would be greatly appreciated.

share|improve this answer
after days of looking at this, fixed it by removing the trim from the posting function. In case this can help anyone... – kayjay Mar 12 '12 at 21:08

Looks like a simple syntax error. I've tidied up the code a bit and use WordPress' checked function. Try:

$sponsorselected = get_post_meta($post->ID, 'eventsponsor', $single = true);
$check =  (!empty($sponsorselected) && in_array($postslug,$sponsorselected) ? true : false);

echo "<input type='checkbox' ".checked($check,true,false)." name='{$this->prefix}{$customField['name']}[]' value='{$postslug}' style='width: auto;border:0;' />"

I've left the styling in, but I'm not sure it does anything..

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.