Support

Account

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

Solved

Cronjob acts only on last post in custom post 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.

    `
    // 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’);
    `

  • Never mind above post

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.