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!