Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';不要在我的PHP代码中看到错误,需要重新审视_Php_Post_Time - Fatal编程技术网

Can';不要在我的PHP代码中看到错误,需要重新审视

Can';不要在我的PHP代码中看到错误,需要重新审视,php,post,time,Php,Post,Time,对于有PHP经验的人来说,这应该是小菜一碟。 我的代码有点问题&在开始/调整代码两天后,我看不出错误 检查下面的更新 当设定时间结束时,这将更改post状态 ($draft_to_deleted_delay) 问题:未更改职位状态 我认为问题在于状态更改代码或日期格式不兼容但我对PHP不是很在行 然而&我不确定 这是所有包装在函数中,该函数是用WP cron调用的-cron works,用插件检查它 没有错误 所有代码: // Covert date input $draft_to_de

对于有PHP经验的人来说,这应该是小菜一碟。 我的代码有点问题&在开始/调整代码两天后,我看不出错误

检查下面的更新


  • 当设定时间结束时,这将更改post状态 ($draft_to_deleted_delay)
  • 问题:未更改职位状态

  • 我认为问题在于状态更改代码或日期格式不兼容但我对PHP不是很在行 然而&我不确定

  • 这是所有包装在函数中,该函数是用WP cron调用的-cron works,用插件检查它

  • 没有错误


所有代码:

// Covert date input
$draft_to_deleted_delay = '00-00-00-00-10-00'; //YY-MM-DD-HH-MM-SS <-- year-month-day-hour-minute-second

$draft_to_deleted_delay     = explode('-', $draft_to_deleted_delay);
$draft_to_deleted_delay     = new DateInterval(
    'P'.$draft_to_deleted_delay[0].'Y'.
    $draft_to_deleted_delay[1].'M'.
    $draft_to_deleted_delay[2].'D'.
    'T'.$draft_to_deleted_delay[3].'H'.
    $draft_to_deleted_delay[4].'M'.
    $draft_to_deleted_delay[5].'S'
);

// Get current date
$now = new DateTime();

// Get all the unpublished posts
$unpublished_posts = new WP_Query(array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'meta_key'          => 'draftDate', //This value is saved as strftime('%F %T') when post gets status "draft"
    'post_status'       => 'draft'
));

while($unpublished_posts->have_posts()) {
    $unpublished_posts->the_post();
    $draft = get_post_meta(get_the_ID(), 'draftDate', true);
    if(!empty($draft)) {

        // Date comparison
        $dt = new DateTime($draft);
        $dt->add($draft_to_deleted_delay);
        if($dt < $now) {

            // Expiration date reached, change to any status, "pending" in this example
            wp_update_post(array('ID' => get_the_ID(), 'post_status' => 'pending'));
        }
    }
}
//转换日期输入
$draft_to_deleted_delay='00-00-00-10-00'//YY-MM-DD-HH-MM-SS-1,
“post_type”=>“post”,
'meta_key'=>'draftDate',//当post获取状态“draft”时,此值保存为strftime('%F%T')
“发布状态”=>“草稿”
));
while($unpublished_posts->have_posts()){
$unpublished_posts->the_post();
$draft=get_post_meta(get_ID(),'draftDate',true);
如果(!空($draft)){
//日期比较
$dt=新日期时间($draft);
$dt->add($draft\u to\u deleted\u delay);
如果($dt<$now){
//已达到到期日期,在此示例中更改为任何状态“待定”
wp_update_post(数组('ID'=>get_the_ID(),'post_status'=>'pending'));
}
}
}
更新:

// Covert date input
$draft_to_deleted_delay = '00-00-00-00-10-00'; //YY-MM-DD-HH-MM-SS <-- year-month-day-hour-minute-second

$draft_to_deleted_delay     = explode('-', $draft_to_deleted_delay);
$draft_to_deleted_delay     = new DateInterval(
    'P'.$draft_to_deleted_delay[0].'Y'.
    $draft_to_deleted_delay[1].'M'.
    $draft_to_deleted_delay[2].'D'.
    'T'.$draft_to_deleted_delay[3].'H'.
    $draft_to_deleted_delay[4].'M'.
    $draft_to_deleted_delay[5].'S'
);

// Get current date
$now = new DateTime();

// Get all the unpublished posts
$unpublished_posts = new WP_Query(array(
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'meta_key'          => 'draftDate', //This value is saved as strftime('%F %T') when post gets status "draft"
    'post_status'       => 'draft'
));

while($unpublished_posts->have_posts()) {
    $unpublished_posts->the_post();
    $draft = get_post_meta(get_the_ID(), 'draftDate', true);
    if(!empty($draft)) {

        // Date comparison
        $dt = new DateTime($draft);
        $dt->add($draft_to_deleted_delay);
        if($dt < $now) {

            // Expiration date reached, change to any status, "pending" in this example
            wp_update_post(array('ID' => get_the_ID(), 'post_status' => 'pending'));
        }
    }
}
现在一切都好了,代码也更新了

  • 这不起作用,令人惊讶的是,40多人没有注意到的主要原因是:“>”面临着错误的方向。
  • 同时,post状态更改错误。

当你很累的时候千万不要编程

我从未使用过Wordpress,因此无法提供任何关于这方面的指导,但您所描述的内容似乎是数据库可以而且应该能够使用事件轻松处理的。 将事件想象成windows中的“计划任务”

数据库中“事件”的基本示例(mySql等)。 你可能想

要在数据库中启用事件,请运行以下命令:
SET GLOBAL event_scheduler=ON

事件调用的存储过程的基本示例

create procedure `spUpdateDrafts`( IN `param_days` INT )
    language sql
    not deterministic
    contains sql
    sql security definer
    comment 'Update drafts'

begin
    /* Perform sql operations, for example: */
    declare days integer default 0;
    set @days=cast(param_days as unsigned);

    update `table` set `draftstatus`=0 
        where `draftstatus`=1 and timestampdiff( day, `draft_created_date`, now() ) > @days;
end
检查了一些文档(…),该行看起来可疑(循环指针更改,不返回任何内容):

我想你想要的是
get\u post()
或其他什么。我只想叫:

wp_update_post(array('ID' => get_the_ID(), 'post_status' => 'pending'));

此函数将使用
get\u post(ID)
收集当前数据,并将两者合并

您的脚本是否实际运行函数
wp\u update\u post()
?首先检查是否可以在db中使用event@AWA我真的不太了解WP,但是如果它是一个php脚本,你可以通过浏览器直接访问它,并在脚本中进行输出。你更正了代码,现在这个问题对于将来阅读它的用户来说没有任何意义。
wp_update_post(array('ID' => get_the_ID(), 'post_status' => 'pending'));