我有一个使用用户输入变量(逗号分隔的数字字符串)运行的函数,用于更新自定义分类法中的术语(按id)和自定义帖子类型。尽管the docs say 我应该使用wp_set_object_terms
, 我只能通过使用wp_set_post_terms
. 以下代码将起作用(使用wp_set_post_terms
但不使用wp_set_object_terms
最后):
if(isset($request[\'custom_tax\'])) {
$customtaxarray = explode(",",$request[\'custom_tax\']);
$only_integers = true;
foreach ($customtaxarray as $testcase) {
if (!ctype_digit($testcase)) {
$only_integers = false;
}
}
if ($only_integers) {
$customtax = $customtaxarray;
} else {
return array(
\'code\' => \'missing_integers\',
\'data\' => array(
\'status\' => 403,
\'message\' => "custom_tax must be one or more (comma separated) integers.",
),
);
}
//update custom_tax
wp_set_post_terms($request[\'cpt_post_id\'], $customtax, \'custom_tax\' );
}