Support

Account

Home Forums Backend Issues (wp-admin) Cronjob only acts on last post in loop

Unread

Cronjob only acts on last post in loop

  • I have a created a custom code for sending out ‘Review Request emails’ via a WordPress cronjob.

    The code works fine but it always ONLY acts on the LAST POST. So even though older posts still match the criteria, it doesn’t send out emails for them.

    Any idea what I can do to change this behavior so the system will send out multiple emails (if the criteria matches)?


    // 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');

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.