Cronjob only acts on LAST POST in custom post WordPress loop

I have created this custom code to send out ‘REVIEW REQUEST EMAILS’ via a WordPress cronjob. The code works and an email is sent out, but ALWAYS ONLY FOR THE LAST POST.

So, even when older posts still match the criteria it will NOT send out any email for these posts.

How can I change this behavior so older posts won’t be ignored and emails for these posts (matching the criteria) will also be send out?

Thanks.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Follow up e-mail met review verzoek
function yl_follow_up_email_review_function() {
$review_days_after = get_field('booking_settings_review_delay', 'booking_settings');
$review_url = get_field('booking_settings_review_url', 'booking_settings');
if ($review_days_after && $review_url) {
$bookings = array(
'posts_per_page' => -1,
'post_type' => 'booking',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'booking_status',
'value' => array('confirmed', 'changed'),
'compare' => 'IN',
),
array(
'key' => 'booking_send_mail',
'value' => 'ja',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $bookings );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Verkrijg aangepaste velden van de huidige booking
$send_email = get_field('booking_send_mail');
$booking_status = get_field('booking_status');
$to = get_field('booking_email');
// Zorg ervoor dat deze velden bestaan in je `booking_settings`
$sender_name = get_field('booking_email_from_name', 'booking_settings');
$sender_email = get_field('booking_email_from_email_address', 'booking_settings');
$subject = get_field('booking_email_subject_review', 'booking_settings');
$message = get_field('booking_email_message_review', 'booking_settings');
$headers = array(
'From: ' . $sender_name . ' <' . $sender_email . '>',
'Content-Type: text/html; charset=UTF-8'
);
// Verzend de e-mail en log eventuele fouten
if (wp_mail($to, $subject, $message, $headers)) {
error_log("Email successfully sent to $to for booking ID: " . $query->post->ID);
} else {
error_log("Failed to send email to $to for booking ID: " . $query->post->ID);
}
// Update with new value.
update_field('booking_status', 'completed');
}
}
// Reset postdata
wp_reset_postdata();
}
}
add_action('yl_follow_up_email_review', 'yl_follow_up_email_review_function');
</code>
<code>// Follow up e-mail met review verzoek function yl_follow_up_email_review_function() { $review_days_after = get_field('booking_settings_review_delay', 'booking_settings'); $review_url = get_field('booking_settings_review_url', 'booking_settings'); if ($review_days_after && $review_url) { $bookings = array( 'posts_per_page' => -1, 'post_type' => 'booking', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'booking_status', 'value' => array('confirmed', 'changed'), 'compare' => 'IN', ), array( 'key' => 'booking_send_mail', 'value' => 'ja', 'compare' => '=', ), ), ); // The Query $query = new WP_Query( $bookings ); // The Loop if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // Verkrijg aangepaste velden van de huidige booking $send_email = get_field('booking_send_mail'); $booking_status = get_field('booking_status'); $to = get_field('booking_email'); // Zorg ervoor dat deze velden bestaan in je `booking_settings` $sender_name = get_field('booking_email_from_name', 'booking_settings'); $sender_email = get_field('booking_email_from_email_address', 'booking_settings'); $subject = get_field('booking_email_subject_review', 'booking_settings'); $message = get_field('booking_email_message_review', 'booking_settings'); $headers = array( 'From: ' . $sender_name . ' <' . $sender_email . '>', 'Content-Type: text/html; charset=UTF-8' ); // Verzend de e-mail en log eventuele fouten if (wp_mail($to, $subject, $message, $headers)) { error_log("Email successfully sent to $to for booking ID: " . $query->post->ID); } else { error_log("Failed to send email to $to for booking ID: " . $query->post->ID); } // Update with new value. update_field('booking_status', 'completed'); } } // Reset postdata wp_reset_postdata(); } } add_action('yl_follow_up_email_review', 'yl_follow_up_email_review_function'); </code>
// Follow up e-mail met review verzoek
function yl_follow_up_email_review_function() {

    $review_days_after  = get_field('booking_settings_review_delay', 'booking_settings');
    $review_url         = get_field('booking_settings_review_url', 'booking_settings');

    if ($review_days_after && $review_url) {

        $bookings = array(
            'posts_per_page'    => -1,
            'post_type'         => 'booking',
            'meta_query'        => array(
                'relation'      => 'AND',
                array(
                    'key'       => 'booking_status',
                    'value'     => array('confirmed', 'changed'),
                    'compare'   => 'IN',
                ),
                array(
                    'key'       => 'booking_send_mail',
                    'value'     => 'ja',
                    'compare'   => '=',
                ),
            ),
        );

        // The Query
        $query = new WP_Query( $bookings );

        // The Loop
        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post();

                // Verkrijg aangepaste velden van de huidige booking
                $send_email     = get_field('booking_send_mail');
                $booking_status = get_field('booking_status');
                $to             = get_field('booking_email');

                // Zorg ervoor dat deze velden bestaan in je `booking_settings`
                $sender_name    = get_field('booking_email_from_name', 'booking_settings');
                $sender_email   = get_field('booking_email_from_email_address', 'booking_settings');
                $subject        = get_field('booking_email_subject_review', 'booking_settings');
                $message        = get_field('booking_email_message_review', 'booking_settings');

                $headers = array(
                    'From: ' . $sender_name . ' <' . $sender_email . '>',
                    'Content-Type: text/html; charset=UTF-8'
                );

                // Verzend de e-mail en log eventuele fouten
                if (wp_mail($to, $subject, $message, $headers)) {
                    error_log("Email successfully sent to $to for booking ID: " . $query->post->ID);
                } else {
                    error_log("Failed to send email to $to for booking ID: " . $query->post->ID);
                }

                // Update with new value.
                update_field('booking_status', 'completed');

            }
        }

        // Reset postdata
        wp_reset_postdata();

    }
}
add_action('yl_follow_up_email_review', 'yl_follow_up_email_review_function');

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật