如何插入带有定制帖子类型的帖子并将其与定制分类相关联?

时间:2013-10-31 作者:bestprogrammerintheworld

我想知道如何插入新帖子。我有一个与自定义分类法(categorycourses)相关的自定义帖子类型(课程)。

我设法插入了一篇带有自定义帖子类型课程的帖子,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);
    }

}  
插入自定义类型课程,但不与任何类别课程((29或30)和33)建立关系。What am I doing wrong?

3 个回复
最合适的回答,由SO网友:bestprogrammerintheworld 整理而成

我会提供一个答案,这样将来有同样问题的人可能会得到帮助。。。

The problem wasn\'t in the actual function. It was about when the taxonomy (categorycourses) was registered...

注册自定义岗位类型课程和课程类别的功能:

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: (这不起作用)

add_action(\'init\', \'convertaddcoursesfromaccess\');

//Hooks for courses and courses-categories
add_action( \'init\', \'courses_with_cats\' );
AFTER: (这确实有效!)

//Hooks for courses and courses-categories
add_action( \'init\', \'courses_with_cats\' );

add_action(\'init\', \'convertaddcoursesfromaccess\');
(注意!!!在第一种情况下,即使帖子类型没有注册,也可以插入自定义帖子类型(课程)。但无法将此帖子与自定义分类法关联wp_set_object_terms())

感谢Rahil Wazir让我意识到WP的功能wp_set_object_terms()wp_set_post_terms() 两者都适用于自定义帖子类型EVEN 如果Wordpress Codex在http://codex.wordpress.org/Function_Reference/wp_set_post_terms :

“此函数仅适用于本机帖子类型。对于自定义帖子类型的分类,请使用wp\\u set\\u object\\u terms()”

我已经确认事实并非如此,上面的两个工作函数都适用于我的自定义帖子类型(课程)。但我使用wp_set_object_terms() 因为我怀疑WP会wp_set_post_terms() 将来不推荐使用(不过这只是一个有保留的猜测)

SO网友:Sriram Sri

尝试更改此行

  wp_set_object_terms( $post_id, array($cat_ids), \'categorycourses\', false);

  wp_set_object_terms( $post_id, $cat_ids, \'categorycourses\', false);
$cat_ids 已经是一个数组,您正在使用该数组再次写入。希望有帮助。

SO网友:Rahil Wazir

尝试更改wp_set_object_terms 具有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
我不知道你为什么wp_set_object_terms 不工作,但wp_set_post_terms 应该有用。

Edited:

从…起wp codex:

也许wp\\u set\\u post\\u terms()是一个更有用的函数,因为它检查值​​, 转换由逗号分隔的分类法,并验证整数中的层次术语。

结束