最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Mathematical operations on custom field values? (updated)

programmeradmin3浏览0评论

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.

The problem is, it isn't working, and I've hit a wall / I'm stumped. The value of the custom field, wpcf-engine-days-to-go field remains at its default of 30. I've updated the code and tried using WP_Query instead of get_posts().

    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()){

    while ( $enginePosts->have_posts()) {

    $engine->the_post();

    // This is the part that I'd like to rule-out
    $daysLeft = genesis_get_custom_field('wpcf-engine-days-to-go');

  /* And this section below too. I'm not sure if the cron job isn't firing, but the database isn't updated. The form creates a post with '30' as the default value of the custom field, and when I run the cron job, it remains 30 in the database */

    update_post_meta(the_id(),'wcf-engine-days-to-go',--$daysLeft);

        }

    }

}

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.

The problem is, it isn't working, and I've hit a wall / I'm stumped. The value of the custom field, wpcf-engine-days-to-go field remains at its default of 30. I've updated the code and tried using WP_Query instead of get_posts().

    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()){

    while ( $enginePosts->have_posts()) {

    $engine->the_post();

    // This is the part that I'd like to rule-out
    $daysLeft = genesis_get_custom_field('wpcf-engine-days-to-go');

  /* And this section below too. I'm not sure if the cron job isn't firing, but the database isn't updated. The form creates a post with '30' as the default value of the custom field, and when I run the cron job, it remains 30 in the database */

    update_post_meta(the_id(),'wcf-engine-days-to-go',--$daysLeft);

        }

    }

}
Share Improve this question edited Jul 7, 2015 at 2:51 CA_ asked Jul 5, 2015 at 23:19 CA_CA_ 131 silver badge7 bronze badges 3
  • 2 Sounds like a complicated countdown. What about storing the final date instead as post meta and just use PHP to calculate the number of days left ( e.g. via DateTime )? – birgire Commented Jul 5, 2015 at 23:41
  • That's certainly an option, but I'm using the field with Gravity Forms to set the value to thirty upon a purchase, or renewal (i.e. updating the value back to 30 upon purchase of a renewal). It's the ease of Gravity Form's ability to work with custom fields that has me leaning that direction. I'm still so new at PHP. – CA_ Commented Jul 5, 2015 at 23:44
  • It sounds that you already got the purchase/renewal dates, so I would just use them instead and skip the wp-cron complication. The DateTime::diff in PHP might help here. – birgire Commented Jul 5, 2015 at 23:54
Add a comment  | 

1 Answer 1

Reset to default 0

use here set type method:

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');
}
发布评论

评论列表(0)

  1. 暂无评论