我创建了一个Cron作业函数。但是,我不能为Cron作业设置条件。基本上,如果模板文件名为page-delete.php
那么,如何使用模板名称设置条件?
<小时>
// custom_schedules_create
function custom_schedules_create( $schedules ) {
$schedules[\'every_three_minutes\'] = array(
\'interval\' => 180,
\'display\' => __( \'Every 3 Minutes\', \'textdomain\' )
);
return $schedules;
}
add_filter( \'cron_schedules\', \'custom_schedules_create\' );
// custom_3minutes_event
if ( ! wp_next_scheduled( \'custom_3minutes_event\' ) ) {
$myLocalGMTTimeEvent = time() + 6*60*60;
wp_schedule_event( $myLocalGMTTimeEvent, \'every_three_minutes\', \'custom_3minutes_event\' );
}
add_action( \'custom_3minutes_event\', \'this_funtion_will_work\' );
// this_funtion_will_work
function this_funtion_will_work() {
if ( is_page_template(\'page-delete.php\') /* Checking If Page from this "page-delete.php" template */ ) {
$the_query = get_posts( array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
));
foreach($the_query as $single_post) {
$id=$single_post->ID;
$myLocalGMTTime = 6*60*60;
$getLocalGMTTime = time() + $myLocalGMTTime;
$getLocalGMTDate = date(\'H:i\', $getLocalGMTTime);
if($getLocalGMTDate == \'18:00\'){
$update_post = array(
\'ID\' => $id,
\'post_status\' => \'private\'
);
wp_update_post($update_post);
}
}
}
}
最合适的回答,由SO网友:Md Sakil Sarker 整理而成
我终于可以做到了。
// custom_schedules_create
function custom_schedules_create( $schedules ) {
$schedules[\'every_three_minutes\'] = array(
\'interval\' => 180,
\'display\' => __( \'Every 3 Minutes\', \'textdomain\' )
);
return $schedules;
}
add_filter( \'cron_schedules\', \'custom_schedules_create\' );
// custom_3minutes_event
if ( ! wp_next_scheduled( \'custom_3minutes_event\' ) ) {
$myLocalGMTTimeEvent = time() + 6*60*60;
wp_schedule_event( $myLocalGMTTimeEvent, \'every_three_minutes\', \'custom_3minutes_event\' );
}
add_action( \'custom_3minutes_event\', \'this_funtion_will_work\' );
// this_funtion_will_work
function this_funtion_will_work() {
$the_query = get_posts( array(
\'post_type\' => \'page\',
\'post_status\' => \'publish\',
));
foreach($the_query as $single_post) {
$id = $single_post->ID;
$myLocalGMTTime = 6*60*60;
$getLocalGMTTime = time() + $myLocalGMTTime;
$getLocalGMTDate = date(\'H:i\', $getLocalGMTTime);
$get_template = get_post_meta( $id, \'_wp_page_template\', true );
if( ( $getLocalGMTDate == \'18:00\' ) && ( \'page-delete.php\' == $get_template )){
$update_post = array(
\'ID\' => $id,
\'post_status\' => \'private\'
);
wp_update_post($update_post);
}
}
}