douxi8119 2014-10-02 18:03
浏览 28

在帖子过期后发送提醒电子邮件

I am using some code i found online to hide a post when an expiration date is set in a custom meta box. I am able to accomplish this just fine, but now i would like to create a function that scans all of my posts and if anyone of them has a date in the past i would like to trigger a reminder email that goes out to an email address i designate using custom fields.

Below is my code. Any help would be appreciated:

//Sends Expirary Email
function plugin_email_reminder() {
    global $post;
    $args = array( 
        'post_type' => 'coupon',
        'p'         => $post_id
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); 

    //Sets Expiration
    $expirationtime = get_post_custom_values('coupon_expiration');
        if (is_array($expirationtime)) {
        $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
        if ( $secondsbetween <= 0 ) {

    // Start the code to send the emails
    $to = get_option('plugin_email_address');
    $subject = get_the_title(). ' Post has expired';
    $headers = 'From: '.get_bloginfo('name').' <no-reply@nomail.com>';
    $message = get_the_title(). ' Post has expired. If you would like it to appear please change the expiration date.';
    wp_mail($to, $subject, strip_tags($message), $headers);
    // End the code to send the emails          

    }           

    endforeach; 
}

I can get the email to trigger when just put the code below in my single.php loop

    $expirationtime = get_post_custom_values('coupon_expiration');
        if (is_array($expirationtime)) {
        $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
        if ( $secondsbetween <= 0 ) {

    // Start the code to send the emails
    $to = get_option('plugin_email_address');
    $subject = get_the_title(). ' Post has expired';
    $headers = 'From: '.get_bloginfo('name').' <no-reply@nomail.com>';
    $message = get_the_title(). ' Post has expired. If you would like it to appear please change the expiration date.';
    wp_mail($to, $subject, strip_tags($message), $headers);
    // End the code to send the emails          

    }

But the email goes out only if i go to that single.php page on my website. I would like this reminder email to be dynamic.

Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀