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'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?

share|improve this question
3  
you made $cats_ids an array, and then you put that array into another array. –  Milo Oct 31 '13 at 14:58
    
@Milo, good spotted! Unfortunately it didn't help. –  bestprogrammerintheworld Oct 31 '13 at 16:37
add comment

3 Answers

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.

share|improve this answer
    
Good spotted, but unfortunately the change didn't work. –  bestprogrammerintheworld Oct 31 '13 at 16:38
add comment
up vote 1 down vote accepted

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.org/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)

share|improve this answer
    
Glad you have figure it out yourself. –  Rahil Wazir Nov 1 '13 at 12:13
    
@RahilWazir - without your answer and comments I wouldn't manage to figure it out! :-) –  bestprogrammerintheworld Nov 1 '13 at 12:55
    
The real question here would be, what is convertaddcoursesfromaccess doing? Otherwise it makes no sense that just reordering the add_action()-parts solved your problem. –  ialocin Nov 1 '13 at 17:16
    
True! I should supplied more code initially. –  bestprogrammerintheworld Nov 2 '13 at 20:24
add comment

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.

share|improve this answer
    
Yes, but the documentation also says... "This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()" –  bestprogrammerintheworld Oct 31 '13 at 17:11
    
No way! I have used the same above method for my custom post type of custom taxonomy. have you tried it? –  Rahil Wazir Oct 31 '13 at 17:14
    
It's true. The documentation say so, but the documentation might be incorrect. I will try it and come back to you! Thanks for your suggestion! :-) –  bestprogrammerintheworld Oct 31 '13 at 17:16
    
Haha it might be incorrect YES!!!, i also read it just now. –  Rahil Wazir Oct 31 '13 at 17:18
    
hehe Actually they both seem to work in the same way. I've traced the return value to this: WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Ogiltig taxonomi ) ) [error_data] => Array ( ) ), so now I'm able to figure what's going on... –  bestprogrammerintheworld Oct 31 '13 at 17:36
show 5 more comments

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.