是否对自定义字段值进行数学运算?(更新版)

时间:2015-07-05 作者:CA_

我正在尝试编写一个函数,由cron计划在每晚午夜运行,该函数将从自定义字段的整数值中减去1。它将基本上起到30天倒计时的作用。

问题是,它不起作用,我撞到墙了/我被难住了。自定义字段的值,wpcf-engine-days-to-go 字段保持默认值30。我已经更新了代码并尝试使用WP_Query 而不是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);

        }

    }

}

1 个回复
SO网友:bhavesh vala

使用此处设置类型方法:

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\');
}

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post