如何在WP Cron作业中通过页面模板名称设置条件?

时间:2020-05-01 作者:Md Sakil Sarker

我创建了一个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);
            }
        }
    }
}

1 个回复
最合适的回答,由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);
        }
    }
}

相关推荐

从在unctions.php外部运行的函数调度cron作业?

我的函数中有一个非常简单的函数和挂钩注册。php,如下所示:function do_this_thingy(){ // I am sending myself some simple admin report mails, tested & works } add_action( \'mail_event_hook\', \'do_this_thingy\'); 然后,在页面模板中,我有一行:wp_schedule_single_event( time()