I registered a custom taxonomy with this code:
register_taxonomy( 'user_r_category', array( 'user_r' ), $args );
Now I try to insert a post to 'user_r_category' taxonomy within the category ID 7 and the post_type 'user_r':
$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);
The post was created, but not with the category 7. How could this work?
You would need to use:
1- get_term_by to get term obj
2- wp_set_object_terms to set custom taxonomy's term with 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.