0

I have created a Cron Job Function. But, I can't set a condition for Cron Job. Basically I want Cron Job need to work if the Template File Name is page-delete.php So, How can I set Condition using Template Name?


// 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);
            }
        }
    }
}
3
  • This would work better if it used a WP_Query loop instead of a get_posts loop – Tom J Nowell May 1 '20 at 18:00
  • kindly can you edit the code? I will helpful for me. – Md Sakil Sarker May 1 '20 at 18:14
  • What do you mean "work if the Template File Name is page-delete.php"? You only want the cron job to run while that template is open? Or you only want to query posts using that template inside the cron job? – Jacob Peattie May 2 '20 at 5:14
0

Finally i can able to achieve it.

// 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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.