I want to setup server cron job for my WordPress website. This is what I have done so far.
I added this line to my wp-config file.
define('DISABLE_WP_CRON', true);
I added this command for setting up cron job.
wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
But my cron function is not working. I setup cron and php function on staging site in a same way and it is working on staging site which is on a different host.
This live website is on bluehost and its default emails are firing as I remove >/dev/null 2>&1
from my command for testing purpose but this PHP function is not working.
Here is my code in functions.php file which is working on staging site but not on live site.
function my_cron_schedules($schedules){
if(!isset($schedules["30min"])){
$schedules["30min"] = array(
'interval' => 30*60,
'display' => __('Once every 30 minutes'));
}
return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
function schedule_my_cron(){
// Schedules the event if it's NOT already scheduled.
if ( ! wp_next_scheduled ( 'my_30min_event' ) ) {
wp_schedule_event( time(), '30min', 'my_30min_event' );
}
}
add_action( 'init', 'schedule_my_cron' );
add_action( 'my_30min_event', 'job_expiration_schedule_hook' );
function job_expiration_schedule_hook() {
$loop = new WP_Query( array( 'post_type' => 'job_listing', 'posts_per_page' => 10, 'post_status' => 'publish') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
$ID = get_the_ID();
$job_post_meta = get_post_meta($ID, '_job_expires', true);
if ( !metadata_exists( 'job_listing', $ID, '_job_expires' ) || empty($job_post_meta)) {
$job_published_date = get_the_time("Y-m-d", $ID);
$expire_date = date('Y-m-d', strtotime($job_published_date. ' + 60 days'));
update_post_meta($ID, '_job_expires', $expire_date);
}
endwhile;
endif;
}