I’m having trouble getting a cron job working in wordpress.
Setup:
I have a custom post type (‘jobs’) that have a couple of meta fields:
‘featured’ (bool) set to true if it is featured and
‘feature-expiry’ (DateTime) which is the time that the feature expires.
Goal:
Once the feature-expiry time has passed, update the post meta and change ‘featured’ to false
I’m kindof new to php and I can’t seem to get it to work. The cron job fires fine but the posts aren’t being updated, what am I doing wrong?
/* jj cron jobs */
function jj_feaurecheck_cron_function( ) {
global $post;
$args = array(
'post_type' => 'jobs',
'posts_per_page' => -1,
);
$listings = get_posts( $args );
foreach($listings as $post) : setup_postdata($post);
$today = date( 'Ymd' );
$expire = get_field( 'feature-expiry', false, false );
$status = get_field( 'featured' );
if ( $expire < $today ) :
$status = 'false';
update_field( 'featured', $status );
endif;
endforeach;
}
if ( ! wp_next_scheduled( 'jj_feaurecheck_cron' ) ) {
wp_schedule_event( date( 'Ymd' ), 'daily', 'jj_feaurecheck_cron' );
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );
2
Answers
[---- Fix ----]
get_field()
andupdate_field()
are for use with Advanced Custom Fields, which my project isn't using. swtiched toget_post_meta()
andupdate_post_meta()
.if ( $expire < $today )
for some reason was affecting posts set to expire in the future so I switched them to unix time and that did the trick.Thank you disinfor and Steven for your guidance, I can't tell you how much I appreciate it! =]
try this, I cannot test it so hopefully there are no errors but this is the general format i use to do what you are trying.