Replies: 0
Hi, I have set the above action to to fire when a custom post is set from published to draft. When I set the post to draft manually, it works as expected, which is to run through a table, select a row randomly and email that person. I have also installed and activate the Post Expirator plugin, which set the post to draft at the end of its cycle. This still fires the action, but for some reason does not run the select query and I can’t find out why. Would some perhaps be able to poitn me in the right direction?
<?php
function intercept_all_status_changes( $new_status, $old_status, $post ) {
if ( $new_status == 'draft' && get_post_type( $post ) == 'competition' ) {
$post = get_the_title();
global $wpdb;
$usernames = $wpdb->get_results("SELECT id, user_login, user_email, comp_title, comp_host, host_email FROM wp_competition WHERE comp_title = '". $post ."' ORDER BY RAND() LIMIT 1");
// Display users in a list
foreach ($usernames as $username) {
$randomuserid = $username->id;
$randomusers4 = $username->user_login;
$randomusersemail = $username->user_email;
$randomusers3 = $username->comp_title;
$randomusers5 = $username->comp_host;
$randomusers6 = $username->host_email;
}
if (!empty($randomusers4)) {
/*$randomusersemail = 'gary@roomworks.co.za';*/
$to = ''.$randomusersemail.'';
$subject = 'We have a winner!';
$message = ' Query Works';
$headers = "From: noreply@company.com" . "\r\n";
$headers .= "Reply-To: noreply@company.com" . "\r\n";
$headers .= "CC: me@home.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to ,$subject,$message,$headers);
if (mail($to, $subject,$message,$headers)) {
$post = get_the_title();
global $wpdb;
$wpdb->query("UPDATE wp_competition SET status= 1 WHERE comp_title='$post'");
}
} else {
$to = 'me@home.com';
$subject = 'We have a winner!';
$message = 'Query Does not Work';
$headers = "From: noreply@company.com" . "\r\n";
$headers .= "Reply-To: noreply@company.com" . "\r\n";
$headers .= "CC: me@home.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to ,$subject,$message,$headers);
}
}
}
add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );