繁体   English   中英

wp_insert_post与现有类别/术语

[英]wp_insert_post with existing categories/terms

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

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

您可以使用功能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 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM