Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a small problem with custom fields. This is what I am trying to do:

  • Whenever a new post is added, I check what category is selected, grab the sub categories and create custom fields with a "0" value based on the sub categories.
  • On each post I grab the sub categories, put them in a form and create checkboxes for each. If the checkbox is checked when the submit button is pressed, the value of the custom field is changed to "1". So I basically toggle the 1 and 0. If the value is 1, I use that particular custom field as a title and add some data to the post, and it also adds checked="yes" to the checkbox.

Everything works great. The only problem I am facing is for custom fields with more than one word. Ex. "Firefox" works, but if the custom field is "Firefox Browser" it won't update. If I manually edit the post and add "1" to the "Firefox Browser" custom field, the checkbox is checked and my div shows up with the "Firefox Browser" title. Unchecking the checkbox and hitting submit changes the value to "0" but checking the checkbox won't change value to "1".

Does anybody have any ideas why this happens?

Thank you!

This is my code:

<?php
 //Get the first category of the post
$post_category = get_the_category();
$args = array('parent' => $post_category[0]->cat_ID, 'hide_empty' => 0);
$categories = get_categories( $args );  

<form action="http://mysite.com/checkform" method="post">


foreach($categories as $category) {
$chkchk = get_post_meta($post->ID, $category->name, true);
if ($chkchk != "0"){$i = 'checked="yes"';}else{$i = "";}
echo '<input type="checkbox"' . $i . 'name='.'"'.$category->name.'"'.'value=""  />'.$category->name;

}

<input type="hidden" name="post_id" value="<?php echo $post->ID; ?>">
<input type="hidden" name="post_cat" value="<?php echo $post_category[0]- >cat_ID; ?>">
<input type="submit" value="Submit" />
</form>
?>

<?php
 //Get hidden
$post_id = $_POST['post_id'];
$post_cat = $_POST['post_cat'];


 //Get current custom field values
$post_catt = get_the_category();
$args = array('parent' => $post_cat, 'hide_empty' => 0);
$cats = get_categories( $args
);


foreach($cats as $cat) {
$checkthis = get_post_meta($post_id, $cat->name, true);

if (isset($_POST[$cat->name])) {
if($checkthis != 1){
update_post_meta($post_id, $cat->name, 1);}
}else{
if($checkthis != 0);{
update_post_meta($post_id, $cat->name, 0);}
}}`
?>

<?php
        foreach($categories as $category) {
                $dropthis = get_post_meta($post->ID, $category->name, true);
                //echo $category->name;
                //echo "=";
                //echo $dropthis;
                $abcd = str_replace (" ", "", $category->name);
                $cat_php = strtolower($abcd);
            if($dropthis != 0){
                include 'drops/'.$cat_php.'.php';
            }

    } ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

'name' is what gets saved as key in the post meta data, so do a str_replace for the category name, before saving:

// Wrong: name="My Category"
echo '<input type="checkbox"' . $i . 'name='.'"'.$category->name.'"'.'value=""  />'.$category->name;

// Right: name="my_category"
$name = str_replace( ' ', '_', strtolower( $category->name ) );
$checked = get_post_meta( ...etc... );
echo "<input type='checkbox'{$i} name='{$name}' value='{$checked}'  />$name;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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