0

I am trying to save custom taxonomies by choosing checkboxes. Normally if it is selectbox, there is no problem. But when I use checkboxes to choose terms it doesn't save. I am suspected that it is an array problem but I could not solve. I request your very kind help. Sorry my bad English. Thank you.

Here is the part to add the post; `

// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title'        =>  $title,
'post_category'     =>  array($_POST['cat']),  // Usable for custom taxonomies too  
'post_status'       =>  'publish',           // Choose: publish, preview, future, draft, etc.
'post_type'         =>  'post',  //'post',page' or use a custom post type if you want to
'tax_input'         =>  array( 'genre' => array($_POST['genre']) )  // support for custom taxonomies.   
);

`

And This is saving the post,

//SAVE THE POST $pid = wp_insert_post($new_post, $wperror); wp_set_post_terms($pid,array($_POST['genre']),'genre',true);

The code below is for my custom taxonomy, genre will be something like music type. `

<?php
$genres = get_terms('genre', 'orderby=id&hide_empty=0');
$counter = 0;
foreach ($genres as $genre) {
$counter++;
$option = '<input type="checkbox" name="genre[]" id="'.$genre->name.'" value="'.$genre->name.'">';
$option .= '<label for="genre">'.$genre->name.'</label>';
echo $option;
}
?>

`

7
  • Try printing the $_POST array to see if you're getting the taxos as genre array. Oct 3, 2011 at 4:46
  • Thank you for yur answer, I already tried that one but it doesnt work. If same foreach loop is select box it works, but when it comes to checkbox problem starts,
    – Baysolucan
    Oct 3, 2011 at 5:26
  • What do you exactly get with the print_r? Try working with debug = true. Oct 3, 2011 at 5:42
  • When I send, print_r, print_r(array($_POST['genre'])); I got this; Array ( [0] => Array ( [0] => Term1 [1] => Term2 [2] => Term3 ) )
    – Baysolucan
    Oct 3, 2011 at 5:54
  • My Wp debug function is also open but I do not get any error. My opinion: array works, but the problem is with checkbox. Colud yu please check foreach loop at the 3rd code.
    – Baysolucan
    Oct 3, 2011 at 5:56

1 Answer 1

1

Change the value of each checkbox to term slug instead of name, and change

wp_set_post_terms($pid,array($_POST['genre']),'gen re',true);

To

 wp_set_post_terms($pid,(array)$_POST['genre'],'gen re',true);
3
  • Thanks [toda :) ] @bainternet. Say, can you add an example with a save button so i could configure it to be saved from the frontend? looking to mark categories / taxmonies in checkboxes then saving them from the front end... (a little example would be very helpfull if possible)
    – Sagive
    Dec 21, 2011 at 12:58
  • Know what.. its a weird comment, thanks for the code example - going to create a question with my recent code and hope for the best... :) - thanks again..
    – Sagive
    Dec 21, 2011 at 13:44
  • Glad I could help, Alo davar :)
    – Bainternet
    Dec 21, 2011 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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