簡體   English   中英

wp_insert_post 帶有自定義分類法

[英]wp_insert_post with custom taxonomy

我使用以下代碼注冊了自定義分類法:

register_taxonomy( 'user_r_category', array( 'user_r' ), $args );

現在我嘗試在類別 ID 7 和 post_type 'user_r' 中插入一個帖子到 'user_r_c​​ategory' 分類法:

$new_post = array(
          //'ID' => '',
          'post_author' => $current_user->ID, 
          //'post_category' => array(7),
          'post_type'   => 'user_r',
          'post_content' => $r_textarea, 
          'post_title' => $r_title,
          'tax_input'    => array(
                            'user_r_category' => array( 7 )
                        ),
          'post_status' => 'publish'
        );

    $post_id = wp_insert_post($new_post);

該帖子已創建,但不屬於類別 7。這如何工作?

您需要使用:

1- get_term_by獲取術語 obj
2- wp_set_object_terms用 post 設置自定義分類的術語

$post_id = wp_insert_post($new_post);
$taxonomy = 'user_r_category';
$termObj  = get_term_by( 'id', 7, $taxonomy);
wp_set_object_terms($post_id, $termObj, $taxonomy);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM