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?
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?
Try changing this line
wp_set_object_terms( $post_id, array($cat_ids), 'categorycourses', false);
to
wp_set_object_terms( $post_id, $cat_ids, 'categorycourses', false);
$cat_ids
is already an array and you are writing with in array again.
Hope it helps.
I'll supply with an answer so people with the same issue in the future might be helped...
The problem wasn't in the actual function. It was about when the taxonomy (categorycourses) was registered...
Function for registerering custom post type course and course-category:
function courses_with_cats() {
//Register custom post type for courses
$course_labels = array(
'name' => 'Kurser',
'singular_name' => 'Kurs',
'add_new' => 'Lägg till',
'add_new_item' => 'Lägg till ny kurs',
'edit_item' => 'Redigera kurs',
'new_item' => 'Ny kurs',
'all_items' => 'Alla kurser',
'view_item' => 'Visa kurs',
'search_items' => 'Sök kurser',
'not_found' => 'Inga kurser funna',
'not_found_in_trash' => 'Inga kurser funna i sopkorgen',
'parent_item_colon' => '',
'menu_name' => 'Kurser'
);
$course_args = array(
'labels' => $course_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'kurs' ),
'capability_type' => 'post',
'has_archive' => 'kurser',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','comments'),
);
register_post_type( 'course', $course_args );
//Register custom taxonomy for courses-categories
$course_cat_labels = array(
'name' => 'Kurskategori',
'singular_name' => 'Kurskategori',
'search_items' => 'Sök kategori',
'all_items' => 'Alla kategori',
'parent_item' => 'Föräldrakategori',
'parent_item_colon' => 'Föräldrakategori:',
'edit_item' => 'Redigera kategori',
'update_item' => 'Uppdatera kategori',
'add_new_item' => 'Lägg till ny kategori',
'new_item_name' => 'Namn på ny kategori',
'menu_name' => 'Kurskategori'
);
$course_cat_args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'kurser/kategori')
);
register_taxonomy( 'categorycourses', array('course'), $course_cat_args );
}
BEFORE: (This did NOT work)
add_action('init', 'convertaddcoursesfromaccess');
//Hooks for courses and courses-categories
add_action( 'init', 'courses_with_cats' );
AFTER: (This did work!)
//Hooks for courses and courses-categories
add_action( 'init', 'courses_with_cats' );
add_action('init', 'convertaddcoursesfromaccess');
(NOTE!!! In first case it worked to insert a custom post type (course) even if the post-type wasn't registered. But it was not possible relate the post with a custom taxonomy with wp_set_object_terms()
)
Thanks to Rahil Wazir that made me aware that WP functions wp_set_object_terms()
and wp_set_post_terms()
both works with custom post types EVEN if Wordpress Codex states otherwise in http://codex.wordpress/Function_Reference/wp_set_post_terms :
"This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()"
I've confirmed that this is NOT the case, both worked functions above worked for me with custom post type (course). But I use wp_set_object_terms()
because my suspicion is that WP will make wp_set_post_terms()
deprecated in the future (this is just a qualified guess though)
Try changing wp_set_object_terms
with wp_set_post_terms
// Set Categories For Current Post
wp_set_post_terms($post_id, array(29, 30, 33), 'categorycourses'); //Change array(29, 30, 33) with your $cat_ids
I don't know why your wp_set_object_terms
not working but wp_set_post_terms
should work.
Edited:
From wp codex:
Perhaps the wp_set_post_terms() is a more useful function, since it checks the values, converting taxonomies separated by commas and validating hierarchical terms in integers.
$cats_ids
an array, and then you put that array into another array. – Milo Commented Oct 31, 2013 at 14:58