PHP WP 用于在只有一个wp_insert_post时创建两个帖子?



我有一个函数,如果管理员接受请求,它将帖子从 x 日期复制到 x 日期。所有这一切都有效。但是,我不是在 FOR 函数中每次运行获得一个帖子,而是获得两个帖子。

我用它来调用下面的函数:

$start = get_field('absence_startdate', $_POST['iPostID']);
$endRepeatDate = get_field('absence_endrepeat', $_POST['iPostID']);
$start = new DateTime($start);
$endRepeatDate = new DateTime($endRepeatDate);
$diff = $endRepeatDate->diff($start);

case 'oddweeks':
$this->duplicatePost(round($diff->days / 7), $_POST['iPostID'], $_POST['sRepeat']);
break;
public function duplicatePost($runtime, $postid, $repeat)
{
for ($increment = 1; $increment <= $runtime; $increment++) 
{   
$org_start = get_field('absence_startdate', $postid);
$org_starttime = get_field('absence_starttime', $postid);
$org_end = get_field('absence_enddate', $postid);
$org_endtime = get_field('absence_endtime', $postid);
$org_note = get_field('absence_note', $postid);
$org_admin = get_field('absence_admin_who', $postid);

$title = get_post($postid)->post_title;
$term = get_the_terms( $postid, 'absence_cat' );

// Duplication starts here
$duplicate = array(
'post_status'   => "publish",
'post_type'     => 'absence',
'post_title'    => '[R: '.$postid.'] GODKENDT - '.$title,
);

switch ($repeat) {
// ---------------------------------------------------------------------------------------------
case 'eachweek':
$dup_start = date('Ymd', strtotime($org_start. '+'.$increment.' week'));
$dup_end = date('Ymd', strtotime($org_end. '+'.$increment.' week'));
break;
// ---------------------------------------------------------------------------------------------
case 'evenweeks':
if(date('W', strtotime($org_start. '+'.($increment + 1).' week'))%2 == 0)
{
$dup_start = date('Ymd', strtotime($org_start. '+'.($increment + 1).' week'));
$dup_end = date('Ymd', strtotime($org_end. '+'.($increment + 1).' week'));
}
else
{
continue;
}       
break;
// ---------------------------------------------------------------------------------------------
case 'oddweeks':
if(date('W', strtotime($org_start. '+'.($increment + 1).' week'))%2 == 1)
{
$dup_start = date('Ymd', strtotime($org_start. '+'.($increment + 1).' week'));
$dup_end = date('Ymd', strtotime($org_end. '+'.($increment + 1).' week'));
}
else
{
continue;
}
break;
// ---------------------------------------------------------------------------------------------
case 'eachmonth':
$dup_start = date('Ymd', strtotime($org_start. '+'.$increment.' month'));
$dup_end = date('Ymd', strtotime($org_end. '+'.$increment.' month'));
break;
// ---------------------------------------------------------------------------------------------
case 'threemonths':
$dup_start = date('Ymd', strtotime($org_start. '+'.($increment + 2).' month'));
$dup_end = date('Ymd', strtotime($org_end. '+'.($increment + 2).' month'));
break;
// ---------------------------------------------------------------------------------------------
case 'sixmonths':
$dup_start = date('Ymd', strtotime($org_start. '+'.($increment + 5).' month'));
$dup_end = date('Ymd', strtotime($org_end. '+'.($increment + 5).' month'));
break;
// ---------------------------------------------------------------------------------------------
case 'eachyear':
$dup_start = date('Ymd', strtotime($org_start. '+'.$increment.' year'));
$dup_end = date('Ymd', strtotime($org_end. '+'.$increment.' year'));
break;
}
$dup = wp_insert_post($duplicate);
wp_set_post_terms( $dup, $term[0]->term_id, 'absence_cat' );
// Org changes
update_field('absence_startdate', $dup_start, $dup);
update_field('absence_enddate', $dup_end, $dup);

update_field('absence_starttime', $org_starttime, $dup);
update_field('absence_endtime', $org_endtime, $dup);
update_field('absence_note', $org_note, $dup);
update_field('absence_admin_who', $org_admin, $dup);
update_field('absence_repeat', $org_repeat, $dup);
update_field('absence_endrepeat', get_field('absence_endrepeat', $postid), $dup);
}
}

请查看案例"偶数周"和案例"奇数周"。它们都重复两次而不是一次。我看不到这个问题。我有一个wp_insert_post,应该只创建一个副本?

如您所见....有两个: https://gyazo.com/1443979280e448defc1a8cd721840a8c

应该只有一个。

解决方案替换:

从我的开关盒:

continue;

continue 2;

为什么?在 PHP 中,switch 语句被认为是一个循环结构,以便继续。继续的行为类似于中断(当没有传递参数时(。如果开关位于循环内,则继续 2 将继续执行外部循环的下一次迭代。

最新更新