I Try to upload and image and assign a taxonomy. The problem is, the term is not saved.
This is the code
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = 'todays_post';
$post_content = $_POST['post_content'];
$new_post = array(
'ID' => '',
'post_author' => $user->ID,
'post_content' => $post_content,
'post_title' => $post_title,
'post_status' => 'draft',
'post_type' => 'dibujos',
);
$post_id = wp_insert_post($new_post);
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $post_id );
}
}
if ($attach_id > 0){
$post = get_post($post_id,'ARRAY_A');
$image = wp_get_attachment_image_src( $attach_id, 'large' );
$image_tag = '<p><img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'" /></p>';
//add image under the content
$post['post_content'] = $image_tag . $post['post_content'];
$post_id = wp_update_post( $post );
// Attach Recipe Taxonomies to created recipe
if(isset($_POST['animales']) && ($_POST['animales'] != "-1") )
wp_set_object_terms( $post_id , intval($_POST['animales']), 'animales' );
}
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( get_permalink($post_id));
exit();
}
What's worng?