functions – Update post meta with wp cron
Question
I am trying to create a function (which I will trigger through the WP Cron) but no values are passed and the database values remain NULL. Could anyone point me in the right direction of where I’m going wrong?
I am trying to compare multiple dates against todays date, keep the future ones, store in an array, and get the first available date in the future -> store this in a new post_meta field. Update the value daily vi WP cron and use this field to sort the posts.
This is my current code with comments:
/*
* ACF Field Structure:
*
* - jurisdiction_release_date (Repeater)
* - jurisdiction_date_selection (Group)
* - alt_release_date (Date Picker)
* - alt_jurisdiction (Taxonomy)
* - alt_release_date_option (Select)
*/
function update_game_release_date() {
$game_args = array(
'post_type'=>'games',
'posts_per_page'=>'-1',
'post_status '=> 'publish',
'fields' => 'ids'
);
// The Query
$the_query = new WP_Query( $game_args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// execute for each post
$current_date = date('Ymd');
$alt_release_dates = array();
$post_id = get_the_ID();
if( have_rows('jurisdiction_release_date') ):
while( have_rows('jurisdiction_release_date') ) : the_row();
//load group
if( have_rows('jurisdiction_date_selection') ):
while( have_rows('jurisdiction_date_selection') ): the_row();
// load field values
$alt_release_date = get_sub_field('alt_release_date');
// only list if jurisdiction date is larger than today's date (in the future)
if ($alt_release_date > $current_date) {
// Add all future dates to the array
array_push( $alt_release_dates, $alt_release_date );
}
endwhile;
endif;
endwhile;
// only future release dates are now stored in an array. Just select the next (smallest) by using min()
//create new variable from array in template above
$release_date = min($alt_release_dates);
else:
// If not dates added, get date value from ACF field 'release_date'
$release_date = get_field('release_date');
endif;
// Update the post meta field '_release_date_hidden' with the new value stored in variable $release_date
update_post_meta( $post_id, '_release_date_hidden', $release_date );
}
} }
// The wordpress Cron
add_action( 'update_cron', 'update_game_release_date' );
if ( !wp_next_scheduled( 'update_cron' ) ) {
wp_schedule_event( time(), 'hourly', 'update_cron' );
}
0
2 months
2022-12-06T08:14:27-05:00
2022-12-06T08:14:27-05:00 0 Answers
0 views
0
Leave an answer