【问题标题】:WordPress Scheduler EmailWordPress 调度程序电子邮件
【发布时间】:2015-09-14 04:12:22
【问题描述】:

我不太擅长 WordPress php 编码和多循环,请大家帮忙。

我正在尝试创建 API 函数调用调度程序邮件。它所做的是检查帖子表以获取 woocommerce 订购的帖子。获取 post_modified 元数据,其中 post_status 为“wc-completed”,并基于 post_modified 日期,该函数将在 post_modified 日期后 5 天后向用户发送一封电子邮件。

public function schedulerMail(){
        global $wpdb;

        $s= $wpdb->get_results("SELECT ID,post_modified FROM $wpdb->posts WHERE post_status like 'wc-completed'");
        for($i = 0; $i < count($s); $i++){
            $post_data = array (
            'ID' => $s[$i]->ID,
            'post_modified' =>$s{$i}->post_modified,        
            );
            return array ("post"=>$post_data);
        }

    }

我想出了这段代码,它确实显示了 $s 的正确数据。

{
0: [
{
ID: "99",
post_modified: "2015-09-02 07:58:14"
},
{
ID: "100",
post_modified: "2015-09-04 02:59:27"
},
{
ID: "101",
post_modified: "2015-09-09 07:36:56"
}
],
status: "ok"
}

但是当我返回 $post_data 时它只返回 1 行数据而不是 3

{
   status: "ok",
   post: {
      ID: "99",
      post_modified: "2015-09-02 07:58:14"
         }
}

我不知道如何解释,但我想根据这些日期进行日期检查,如果日期正好超过 5 天,请发送电子邮件。

【问题讨论】:

  • 如我所见,您每次都在循环中重新创建/覆盖数组

标签: php wordpress email


【解决方案1】:

您在循环本身中编写了返回数组。因此程序在第一次迭代中退出循环。

public function schedulerMail(){
    global $wpdb;
    $s= $wpdb->get_results("SELECT ID,post_modified FROM $wpdb->posts WHERE post_status like 'wc-completed'");
    $post_data = array();
    for($i = 0; $i < count($s); $i++){
        /*$post_data = array (
        'ID' => $s[$i]->ID,
        'post_modified' =>$s{$i}->post_modified,
        );*/
        $post_data[$i]['ID'] = $s[$i]->ID;
        $post_data[$i]['post_modified'] = $s{$i}->post_modified;           
    }
    return array ("post"=>$post_data);
}

你想要什么:

public function schedulerMail(){
        global $wpdb;
        $s= $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_status like 'wc-completed' AND DATE_SUB(CURDATE(),INTERVAL 10 DAY) <= post_modified");
        for($i = 0; $i < count($s); $i++){
            $post_data[$i]['ID'] = $s[$i]->ID;
            //$this->mailout($post_data[$i]['ID']);

        }
        return array("ID"=> $post_data);
    }

【讨论】:

  • 我非常感谢它现在可以工作了。好的,现在我的每个帖子和日期都正确我想检查这些日期,如果发送的时间超过 5 天,我应该开始插入代码吗?
  • 我想,你想要的帖子少于 5 天。在这种情况下,为 Post_Modified 字段添加 where 子句,例如: where post_modified
  • erm 我的意思是,例如现在我们调用这个 schedulermail() 并且我们知道它给了我们这个结果 { status: "ok", post: [ { ID: "99", post_modified: "2015- 09-02 07:58:14”},{ ID:“100”,post_modified:“2015-09-04 02:59:27”},{ ID:“101”,post_modified:“2015-09-09 07 :36:56" } ] } 因此对于每个结果,如果当前日期是每个给定结果之后的 5 天,它将获取帖子 ID 并发送一封包含该 ID 数据的邮件。
  • 建议您在 SQL 中应用 where 子句,而不是检查每个帖子的日期。这将确保所有数据都少于 5 天,例如 '$s= $wpdb->get_results("SELECT ID,post_modified FROM $wpdb->posts WHERE post_status like 'wc-completed' 和 post_modified
【解决方案2】:

感谢 Ankur140290

public function schedulerMail(){
        global $wpdb;
        $s= $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_status like 'wc-completed' AND DATE_SUB(CURDATE(),INTERVAL 10 DAY) <= post_modified");
        for($i = 0; $i < count($s); $i++){
            $post_data[$i]['ID'] = $s[$i]->ID;
            //$this->mailout($post_data[$i]['ID']);

        }
        return array("ID"=> $post_data);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多