Create a Daily Digest Email of New Posts Sent to Subscribers in WordPress (with PHP & wp_cron)
Want to keep your users engaged without bombarding them with emails every time a new post goes live? A daily digest email is the perfect solution — it summarizes all published content within the last 24 hours and sends it automatically to your subscriber list.
In this tutorial, you'll learn how to build a lightweight digest system in WordPress using wp_cron
and native wp_mail()
— no third-party plugins required.
🧩 What You’ll Build
-
A
wp_cron
task that runs daily -
A query to fetch all posts published in the last 24 hours
-
A formatted HTML email with post titles and links
-
An email delivery function sent to admin or subscribed users
✅ Step 1: Schedule the Daily Cron Event
You’ll first set up a recurring task using wp_schedule_event
.
function setup_daily_digest_cron() {
if (!wp_next_scheduled('send_daily_digest_event')) {
wp_schedule_event(time(), 'daily', 'send_daily_digest_event');
}
}
add_action('wp', 'setup_daily_digest_cron');
🔍 Step 2: Fetch Posts Published in the Last 24 Hours
Use WP_Query
to retrieve content between now and yesterday:
function get_posts_from_last_day() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'date_query' => array(
array(
'after' => '24 hours ago',
),
),
);
return get_posts($args);
}
Step 3: Format the Digest Email
Build a basic HTML layout with post titles and links:
function generate_daily_digest_body($posts) {
if (empty($posts)) {
return false;
}
$output = '<h2>Daily Digest - Latest Posts</h2><ul>';
foreach ($posts as $post) {
$title = esc_html(get_the_title($post->ID));
$link = esc_url(get_permalink($post->ID));
$output .= "<li><a href='{$link}'>{$title}</a></li>";
}
$output .= '</ul>';
return $output;
}
Step 4: Send the Email
Now put it all together and send the email using wp_mail()
.
function send_daily_digest_email() {
$posts = get_posts_from_last_day();
$body = generate_daily_digest_body($posts);
if (!$body) {
return;
}
$to = get_option('admin_email'); // or your custom subscriber list
$subject = 'Your Daily Blog Digest';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($to, $subject, $body, $headers);
}
Step 5: Testing and Customization
You can test the function manually:
// Run it once manually for testing
// send_daily_digest_email();
Optional enhancements:
-
Add a settings page to let admins customize recipients
-
Use a custom user role or checkbox to opt-in users
-
Track delivery or open rate with UTM links
-
Send different digests per category or tag
🧠 Summary
This simple system runs daily and emails your site’s latest posts automatically — perfect for:
-
Blogs
-
News portals
-
Member communities
-
Intranets
Convenient hosting for your WordPress sites
Looking for good hosting for your WordPress sites? Pay attention to Host4Biz. It is a reliable hosting with modern servers in Europe and a Ukrainian team.
And with the promo code MYHOST10 you will get a 10% discount on your first payment. To do this, register here and enter the code before paying.
Note: There are affiliate links in the link given above and if you buy something, I’ll get a commission at no extra cost to you.
Read also: Best Digital Agency WordPress Themes (Mostly Free) https://medium.com/@wwwebadvisor/best-digital-agency-wordpress-themes-mostly-free-a4f64e0bd03f
Comments
Post a Comment