I have a function in my functions.php file that is supossed to update my post status once the expiry date has passed. I call this function in a test.php file that is also a template page with url /test-search-results/. If I access this url via my browser the function fires and does what it supposed to do.
I want to create a server side cronjob to do this but I cant seem to get this right. I have contacted my host and they confirm that the path is correct and that the cron is executing si the problem has to be with my script.
Can someone please advise what I have to change in my script to have the cronjob execute it.
Function in my functions.php file:
function expire_posts () {
$args = array (
'post_type' => 'post',
'meta_key' => 'Expiry Date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<',
);
$query = new WP_Query($args);
if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$post_data = array(
'ID' => $post_id,
'post_type' => 'post',
'post_status' => 'expired',
'post_modified' => current_time ( 'mysql' ),
);
wp_update_post($post_data);
endwhile;
wp_reset_postdata();
endif;
}
I call the function in my test.php file like this:
expire_posts();
The path my cronjob is following is:
/home/userID/public_html/wp-content/themes/twentyseventeen-child/test.php
I am hoping someone can shed some light as to why my script isn’t executing via the cronjob.