繁体   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