更新答案很简单,一开始我看不到。:)
只需在第一次函数调用期间删除该操作。这样,你的工作within the API, 函数只被调用一次。不需要静态甚至全局变量或常量。
function my_metabox_save() {
remove_action( \'save_post\', \'my_metabox_save\' );
// go on with your function ...
我将保留旧的答案,以说明如果您认为太抽象,解决方案可能会变得多么尴尬…
旧答案添加检查my_metabox_save()
阻止第二次呼叫。
示例代码(未测试):
function my_metabox_save() {
static $done = FALSE;
if ( $done )
{ // No, not again!
return;
}
//data authenticity check
//process & sanitize data
//create posts
for( $i=0; $i<$count; $i++ ) {
$args = array(
\'post_status\' => \'pending\',
\'post_title\' => $_POST[\'post_title\'][$i],
\'post_type\' => \'custom_post_type\',
);
foreach( $category_array[$i] as $category ) {
$args[\'tax_input\'][\'custom-taxonomy\'][] = $category;
}
if( $_POST[\'id_of_previously_created_post\'][$i] != \'\' ) {
$args[\'ID\'] = $_POST[\'id_of_previously_created_post\'][$i];
unset( $args[\'post_status\'] );
}
$new_post_id = wp_insert_post( $args );
}
$done = TRUE; // Remember that we’re done.
}