[英]Wordpress, add post to custom taxonomy's category
我正在嘗試在分類學類別下的類別中添加帖子。 我使用的代碼是:
$user = get_user_by( 'email', $_POST['user'] );
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'taxonomy' => ('cate'),
'post_type' => 'ad',
'post_category' => array(425),
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
帖子已添加,但添加在“未分類”類別下,而不是所需的類別ID下。當不使用自定義帖子類型時,此系統可以正常工作(在這種情況下,分類法為“類別”)
有任何想法嗎?
您需要wp_set_object_terms ,該參數以帖子ID,術語,分類法和附加作為參數。 例如:
$user_id = wp_insert_post( $id );
wp_set_object_terms( $user_id, 'cate', 'category', true );
我這樣解決了:
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'post_type' => 'ad',
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
wp_set_object_terms($user_id, 416, 'cate', true);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
約什(Josh)向我展示了方法,但是他的語法不正確。 首先是類別,然后是分類法,必須刪除一些內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.