这是一种独特的情况。我有一个开票自定义帖子类型,当它标记为“paid”时,我想生成另一个自定义帖子类型。我有一个处理发票元的自定义元文件,我正在尝试使此功能正常工作。目前,它确实有效,但它创建了无限多的新帖子,所有帖子都具有相同的标题等等。我必须迅速停止该页面,它已经有近1000篇帖子了。这是我当前的函数,希望有人能看到我做错了什么。当我在某个地方读到可以消除循环的内容时,我确实尝试将该帖子添加为未来的帖子。。。不起作用。
// Generate CEU if payment is entered manually
function generate_ceu( $post_id ){
// check nonce
if (!isset($_POST[\'invoice_meta_box_nonce\']) || !wp_verify_nonce($_POST[\'invoice_meta_box_nonce\'], \'invoice_meta_box\')) {
return $post_id;
}
// check capabilities
if (\'invoice\' == $_POST[\'post_type\']) {
if (!current_user_can(\'edit_post\', $post_id)) {
return $post_id;
}
} elseif (!current_user_can(\'edit_page\', $post_id)) {
return $post_id;
}
// exit on autosave
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post_id;
}
// Check if the payment amount already exists so we don\'t create another CEU
if (!get_post_meta($post->ID, \'invoice_payment_amount\', true)) {
if (isset($_POST[\'invoice_payment_amount\'])) {
$title = \'CEU\'.date(\'YmdHs\');
// Create the post
$new_post = array(
\'post_title\' => $title,
\'post_date_gmt\' => strtotime(\'+5 minutes\'), // Set the post publish time to 5 minutes from now so it wont loop
\'post_status\' => \'future\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => \'ceu\', //\'post\',page\' or use a custom post type if you want to
);
$pid = wp_insert_post($new_post);
} // end check for post entry
} // end check for meta data
} // end function
add_action( \'save_post\', \'generate_ceu\');