获取刚刚添加的term_id(WordPress)

时间:2019-08-24 10:45:40

标签: php wordpress taxonomy

我有一个前端表单,并且正在使用此方法添加分类法和描述:

$job_name_description = array(
    'cat_name'              => $people, 
    'category_description'  => $people_description,
    'taxonomy'              => 'job'
);  
wp_insert_category( $job_name_description );

这很好。

但是在该代码运行之后,我现在需要获取刚刚添加的term_id。

例如,如果运行该代码后,我在数据库中看到了此信息...

enter image description here

...然后我需要知道term_id是什么(在此示例中为38)并将其设置为变量。

2 个答案:

答案 0 :(得分:0)

wp_insert_category()返回类别ID。

$cat_id = wp_insert_category( $job_name_description );

然后使用get_term_by()获得术语ID:

get_term_by_id('id', $cat_id, 'job');

答案 1 :(得分:0)

我知道了;

$job_name_description = array(
    'cat_name'              => $people, 
    'category_description'  => $people_description,
    'taxonomy'              => 'job'
);  
$result = wp_insert_category($job_name_description); 
$term_id = $result;
echo 'The term ID is ' . $result;

这是因为,如@suspectus所指出的,wp_insert_category返回类别ID(http://hookr.io/functions/wp_insert_category/)。