In detail the condition is There are multiple new users to whom admin want to send welcome message but only once for each member. I have done sending message but if the member refresh his email page than the code execute again and new message is send to him by admin and i want to stop sending multiple mail even after he reload his mail page.

$rcvr_id = $wpdb->get_results("SELECT ID FROM wp_users ORDER BY ID "); foreach($rcvr_id as $rv_id) { $rcvr_id = $rv_id->ID;

global $wpdb;
$table_name = $wpdb->prefix . "mail_messages";
$date = date('Y-m-d H:i:s');

$rows = array(
        'sender_id' => 4, // who send mail to new members
        'receiver_id' => $rcvr_id, // new members who recieve mail just once from sender above
        'sent_date' => $date,
        'thread_id' => 1,
        'subject' => 'Welcome',
        'text_message' => 'You are welcome to our world.',
        'send_del_message' => 0

foreach( $rows as $row )

    $sndr_id = $wpdb->get_results("SELECT sender_id FROM wp_mail_messages ");
//echo $sndr_id;

    $wpdb->insert( $table_name, $row);

if($sndr_id != 4){



This is code which send mail to all members but continue sending on each refresh of page by receiver.

1 Answer 1


It has probably more systematical solution, but one option is to save the highest recipient ID, and then load only higher IDs next time. Something like this: (not tested)

$last_id = get_option('last_recipient_id');

$rcvr_id = $wpdb->get_results( $wpdb->prepare("SELECT ID FROM wp_users WHERE ID > %d ORDER BY ID "), $last_id ) ; 
foreach($rcvr_id as $rv_id) { 
    $rcvr_id = $rv_id->ID; 

    // your actions

} // end of foreach

// if some result was loaded, $rcvr_id holds the last ID value
if( $rcvr_id )
    update_option( 'last_recipient_id' , $rcvr_id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.