看来term_exists()
正在为已存在的术语返回NULL:
$name = "כל האזורים"
$taxonomy_name = "geographictermsisrael"
$args = array("slug" => "everything", "parent" => 0)
var_dump(term_exists($name, $taxonomy_name, $args)); // Returns NULL (not 0)
if ( !term_exists($name, $taxonomy_name, $args) ) {
$foo = wp_insert_term($name, $taxonomy_name, $args);
var_dump($foo); // Returns the array below
}
object(WP_Error)#4193 (2) {
["errors"]=>
array(1) {
["term_exists"]=>
array(1) {
[0]=>
string(77) "השם או מזהה הכתובת כבר קיימים עם ההורה הזה."
}
}
["error_data"]=>
array(1) {
["term_exists"]=>
string(3) "699"
}
}
If the term already exists, why might term_exists()
return NULL? 这是数据库的外观:
mysql> select * from wp_term_taxonomy where taxonomy=\'geographictermsisrael\';
+------------------+---------+-----------------------+-------------+--------+-------+
| term_taxonomy_id | term_id | taxonomy | description | parent | count |
+------------------+---------+-----------------------+-------------+--------+-------+
| 738 | 699 | geographictermsisrael | | 0 | 0 |
+------------------+---------+-----------------------+-------------+--------+-------+
1 row in set (0.00 sec)
mysql> select * from wp_terms where term_id=699;
+---------+---------------------+------------+------------+
| term_id | name | slug | term_group |
+---------+---------------------+------------+------------+
| 699 | כל האזורים | everything | 0 |
+---------+---------------------+------------+------------+
1 row in set (0.00 sec)
mysql> select * from wp_term_relationships where term_taxonomy_id=738;
Empty set (0.00 sec)
最合适的回答,由SO网友:s_ha_dum 整理而成
的第三个参数term_exists()
是整数,而不是数组。
参数检查术语默认值:无$父项(整数)(可选)$父项的父项ID限制现有搜索的默认值:0
传递正确的参数,它就会工作。
var_dump(term_exists($name, $taxonomy_name));
0
是默认值,因此无需指定它。