I'm trying to create an email alert every monday at midnight that will send the number of posts published the past week on the site.
I created 2 variables: $post_published_to_date
which is the total count of the posts on the site (I assumed here that the site was just created to there aren't any posts yet) and $published_posts
which is the number of posts published during the past week.
I used date('D', $timestamp) === 'Mon' && $timestamp = strtotime('midnight')
in the if
statement to find out if it's monday at midnight. If it is, an email is sent to the admin with the number of posts published.
I'm not sure is setting $published_posts
to 0 at the end will work well, and I'm afraid $post_published_to_date
will be reset to 0 each time. I'm beginning in Php so I'm not really sure here.
<?php
$post_published_to_date = 0;
$published_posts = wp_count_posts()->publish;
if(date('D', $timestamp) === 'Mon' && $timestamp = strtotime('midnight')) {
$post_published_to_date = $post_published_to_date + $published_posts;
add_action( 'publish_post', 'number_of_posts_published', 10 ,2 );
function number_of_posts_published( $ID, $post, $published_posts; ) {
if ( 'post' !== $post->post_type)
return;
$to = '[email protected]'
$subject = 'Number of posts last week';
$headers = array('Content-Type: text/html' );
$message = '<h3> Hello ! </h3> <p>
Last week, $published_posts posts were published on your site. ';
wp_mail( $to, $subject, $message, 'Content-Type: text/html' );
}
$published_posts = 0;
}
?>
Would this work as expected ? Would you advise any modification that are better practices ?
Thanks for your time and help