• 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 = '{email redacted}';*/
    
    $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 );
    
    • This topic was modified 6 years, 8 months ago by bcworkz. Reason: redact email

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m sure the query runs if the action fires and status and post type are correct. It may fail to return expected results, but it runs 🙂 The problem is in getting the title. You are relying upon the global post object that is set in a standard WP loop. It’s apparently not set when the plugin transitions the post.

    You need to get the title of the passed object, not the current loop object.
    $post = get_the_title( $post );

    Thread Starter GaryKay

    (@garykay)

    Thank you @bcworkz , that was the issue, in me trying to sort out another problem I had removed it and forgot to put it back. No to figure why it send duplicate mails now.

    Moderator bcworkz

    (@bcworkz)

    It’s quite common for actions to fire more often than expected. It usually doesn’t matter, doing the identical thing over again usually has no visible impact. When there is an impact, you should always take measures to ensure the callback only runs once. Usually, having your callback remove itself from the action stack will accomplish this, provided it’s not needed more than once per request.

    Thread Starter GaryKay

    (@garykay)

    Thank you for your help. It can wait, have other important features to do first.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘transition_post_status’ is closed to new replies.