Home › Forums › Backend Issues (wp-admin) › 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’);
`
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.