• Hello all,

    I’m working on a cron job that will subtract 1 from a custom field value every 24 hours from a custom post type. The custom field value has a default value of 30. It will function as a count-down. I’m choosing to do it with a custom field because it integrates nicely with Gravity Forms & Woo Commerce (for renewal purposes).

    The problem is.. it isn’t working. I’m using WP Crontrol to fire the cron manually, and the value remains defaulted at 30. Any idea why this isn’t working?

    add_action( 'engineCronHook', 'engineDaysToGoCountdown' );
    if( !wp_next_scheduled( 'engineCronHook' ) ) {
    wp_schedule_event( time(), 'daily', 'engineCronHook' );
    }
    
    // Countdown function
    
    function engineDaysToGoCountdown(){
    
    // Set the post args
    $args = array(
    
        'post_type' => 'engine',
        'posts_per_page' => -1,
        'post_status' => 'publish'
    
        );
    
    //Create enginePosts object
        $enginePosts = new WP_Query($args);
    
    if($enginePosts->have_posts()){
    
        foreach ( $enginePosts as $key => $value) {
    
        $enginePosts->the_post();
        $daysLeft = genesis_get_custom_field('wpcf-engine-days-to-go');
        $daysLeftUpdated = $daysLeft--;
        update_post_meta(get_the_ID(),'wcf-engine-days-to-go',$daysLeftUpdated);
    
            }
    
        }
    
    }
    
    wp_reset_postdata();
  • The topic ‘Incremental modifying custom field value via cron?’ is closed to new replies.