0 votes
in Website Design by (1.9k points)

I'm attempting to write a function, run by a cron schedule at midnight every night, that will subtract 1 from the integer value of a custom field. It will function basically as a countdown timer for 30 days.

My question is if get_posts is the correct query to use, and if this is code is done properly to subtract 1 from the value of the custom field for each post that has that field?

    add_action('init','engineCreateRecurringSchedule');
add_action('engineRecurringCronJob','engineDaysToGoUpdate');


function engineDaysToGoUpdate(){

    // Arguments to get published posts with 'engine' post type.
$engineDaysToGoArgs = get_posts( array (
    'post_status' => 'publish'
    'posts_per_page' => -1,
    'post_type' => 'engine') );

    // Calling the value of custom field.
$engineDaysToGo = genesis_get_custom_field('wpcf-engine-days-to-go');

    // Subtracting 1 from the value.
$updatedEngineDaysToGo = $engineDaysToGo--;

    // Updating the value of the custom field.
for each ($engineDaysToGoArgs as $key => $value) { 

    // Inserting the updated value of the custom field.    
    $update_post_meta($engineDaysToGoArgs, $engineDaysToGo, $updatedEngineDaysToGo,);

    }}

function engineCreateRecurringSchedule(){

   // Check to see if event is scheduled before.
  if(!wp_next_scheduled('engineRecurringCronJob'))

   //Schedule to run at midnight every night.
   wp_schedule_event (time(), 'daily', 'engineRecurringCronJob');
}

can anyone help to solve my problem?

 

1 Answer

0 votes
by (2.4k points)

use here set type method:

for more information please visit below link click here

add_action('init','engineCreateRecurringSchedule');
add_action('engineRecurringCronJob','engineDaysToGoUpdate');


function engineDaysToGoUpdate(){

    // Arguments to get published posts with 'engine' post type.
$engineDaysToGoArgs = get_posts( array (
    'post_status' => 'publish'
    'posts_per_page' => -1,
    'post_type' => 'engine') );

    // Calling the value of custom field.
$engineDaysToGo = genesis_get_custom_field('wpcf-engine-days-to-go');

settype($engineDaysToGo, "integer");

    // Subtracting 1 from the value.
$updatedEngineDaysToGo = $engineDaysToGo--;

    // Updating the value of the custom field.
for each ($engineDaysToGoArgs as $key => $value) { 

    // Inserting the updated value of the custom field.    
    $update_post_meta($engineDaysToGoArgs, $engineDaysToGo, $updatedEngineDaysToGo,);

    }}

function engineCreateRecurringSchedule(){

   // Check to see if event is scheduled before.
  if(!wp_next_scheduled('engineRecurringCronJob'))

   //Schedule to run at midnight every night.
   wp_schedule_event (time(), 'daily', 'engineRecurringCronJob');
}
...