I'm trying to figure out how to insert a new post. I have a custom post-type (course) that is related to a custom taxonomy (categorycourses).
I've managed to insert a post with custom post type course, but I can't figure out how to relate this post to custom taxonomy categorycourses.
//This will return array of category-courses (temporarily tables kurser and kategorier created by me)
$courses = $wpdb->get_results( "SELECT * FROM `kurser` kur
LEFT JOIN kategorier kat ON (kat.id = kur.kategori)" );
//Go through category-courses and put them into wordpress-tables
foreach($courses as $course) {
$name = $course->kursnamn;
//If name has length
if (strlen($name)>0) {
$new_post = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'course'
);
//This works because an ID is returned
$post_id = wp_insert_post($new_post);
$current_term_category = 33;
if (substr($name, 0, 3) == 'lid') {
$cat_city = 29;
}
else {
$cat_city = 30;
}
$cat_ids = array($current_term_category, $cat_city);
//term_id 30,29 and 33 exists in wp_terms-table
//This is where I've done something wrong.
wp_set_object_terms( $post_id, $cat_ids, 'categorycourses', false);
}
}
The custom types courses are inserted but no relations to any categorycourses ((29 or 30) and 33) are made. What am I doing wrong?
$cats_ids
an array, and then you put that array into another array. – Milo Oct 31 '13 at 14:58