0

I have a custom post type called cpt1.

I need to run a function that on a daily base Loops through cpt1 and then check the dates. This is done by using cron job to trigger.

Within this loop it needs to check if the dates is equal to today's date and then automatically make new post under 3 different custom post types based on the date.

30 days

90 days

5 months

here is the code I'm currently using but it does not seem to work any help would be appreciated.

<?php
////////////////////////////////////////////////////////////////////
//  Scheduling a recurring task via Cron Job
////////////////////////////////////////////////////////////////////
add_action( 'mf_cron_job_daily_check_to_create_automatic_posts',             
'create_automatic_posts' );
if ( ! wp_next_scheduled(     
'mf_cron_job_daily_check_to_create_automatic_posts' ) ) {
wp_schedule_event( strtotime('6:00:00'), 'daily', 
'mf_cron_job_daily_check_to_create_automatic_posts' );
}
////////////////////////////////////////////////////////////////////
//  Function
////////////////////////////////////////////////////////////////////
function create_automatic_posts() {
$args = array(
'post_type' => 'ctp1',
);
$loop = new WP_Query($args);
while($loop->have_posts()){
$title = esc_html( get_the_title() );
$ctp1_chapter = get_post_meta(get_the_ID(), '_ctp1_chapter', true);                                                         
$start_date = get_post_meta(get_the_ID(), '_ctp1_starding_date', true);
$day_30 = date('Y-m-d', strtotime($start_date . ' +30 day'));
$day_90 = date('Y-m-d', strtotime($start_date . ' +90 day'));
$month_5 = date('Y-m-d',strtotime($start_date . ' +5 months'));
$today = date("Y-m-d");
if (($day_30 === $today)){ 
$day_30_post = array(
'post_title'     => $title, // The title of your post.
'post_status'    => 'publish', // Default 'draft'.
'post_type'      => '30_days' // Default 'post'.
);  
wp_insert_post($day_30_post);
}
if (($day_90 === $today)){ 
$day_90_post = array(
'post_title'     => $title, // The title of your post.
'post_status'    => 'publish', // Default 'draft'.
'post_type'      => '90_days' // Default 'post'.
);  
wp_insert_post($day_90_post);
}
if (($month_5 === $today)){ 
$month_5_post = array(
'post_title'     => $title, // The title of your post.
'post_status'    => 'publish', // Default 'draft'.
'post_type'      => 'month_5' // Default 'post'.
);  
wp_insert_post($month_5_post);
}
}
}
?>

Your Answer

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

Browse other questions tagged or ask your own question.