我正在编写一个插件并尝试在 foreach 循环内的 wp_term_relationships 表中插入一个新行。由于 var_dump,我知道变量具有值,但由于某种原因,我一直收到错误。这在 show_errors() 函数上显示了大约 600 次:
WordPress 数据库错误:[键 1 的重复条目 '0-0'] INSERT INTO
wp_term_relationships
(object_id
,term_taxonomy_id
,term_order
) VALUES ('','','')
我的代码:
foreach ($cb_t2c_cat_check as $values) {
global $wpdb;
$prefix = $wpdb->prefix;
$table = $prefix . 'term_relationships';
$object_id = $values->object_id;
$taxo_id = $values->term_taxonomy_id;
$num_object_id = (int)$object_id;
$num_taxo_id = (int)$taxo_id;
//var_dump($num_object_id); //This produces values, so why are they not getting inserted into the table?
//var_dump($num_taxo_id); //This produces values, so why are they not getting inserted into the table?
$wpdb->insert(
$table,
array(
'object_id' => $num_object_id,
'term_taxonomy_id' => $num_taxo_id,
'term_order' => 0
), ''
);
//$wpdb->show_errors();
//$wpdb->print_error();
}