wp_insert_post包含现有类别/字词

时间:2017-11-30 07:03:27

标签: php wordpress custom-post-type categories

我正在使用wp_insert_post将一个CPT的帖子插入到另一个CPT并挂钩到add_action('wp', 'function_name')

如何在创建时成功插入带有条款/类别(例如“类型1”)“勾选”的新帖子?主要目的是拥有一个友好的URL,例如domain_name/cat1/cat2/post_name

1 个答案:

答案 0 :(得分:0)

您可以使用功能wp_set_post_terms

wp_set_post_terms( $post_id, $term, $taxonomy );

文件说明:

$tag = '5'; // Wrong. This will add the tag with the *name* '5'.
$tag = 5; // Wrong. This will also add the tag with the name '5'.
$tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.

$tag = array( 5 ); // Correct. This will add the tag with the id 5.
wp_set_post_terms( $post_id, $tag, $taxonomy );