I have been trying to run a function at first of every month, and first of every year. And i wanted to run at 12am exact. Below I have shown the function. When is schedule it for daily it works perfectly, but if I try to run it monthly or yearly, it breaks.
I feel that it can be that there is no facility to run yearly. Nevertheless below is the code, please help me run it at start of each month and each year
function Milyin_Admin_Mail(){ $authors=get_users(); foreach($authors as $author){
$author_posts = get_posts( array('author' => $author->id, 'numberposts' => -1 ));
// needed to collect the total sum of views
$counter = 0;
$word_count=0;
$image_count=0;
$comment_count=0;
$monthly_Views= date("m-Y") . '-Views';
$monthly_Words=date("m-Y") .'-Words';
$monthly_Images=date("m-Y") .'-Images';
$monthly_Comments=date("m-Y") .'-Comments';
$monthly_View_Pay=date("m-Y") .'-View-Pay';
$monthly_Word_Pay=date("m-Y") .'-Word-Pay';
$monthly_Image_Pay=date("m-Y") .'-Image-Pay';
$monthly_Comment_Pay=date("m-Y") .'-Comment-Pay';
$monthly_Total_Pay=date("m-Y") .'-Total-Pay';
// do stuff to get author name
foreach ( $author_posts as $post ) {
$views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) );
$word_count = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
$image_count = count( get_attached_media( 'image', $post->ID ) );
$comment_count = get_comments_number($post->ID) ;
$counter += $views;
$word_counter += $word_count;
$image_counter += $image_count;
$comment_counter += $comment_count;
}
$id= $author->id;
update_user_meta($id, $monthly_Views, $counter);
update_user_meta($id, $monthly_Words, $word_counter);
update_user_meta($id, $monthly_Images, $image_counter);
update_user_meta($id, $monthly_Comments, $comment_counter);
$View_Pay = ($counter/1000)*400;
update_user_meta($id, $monthly_View_Pay, $View_Pay);
$Word_Pay= ($word_counter/1000)*10;
update_user_meta($id, $monthly_Word_Pay, $Word_Pay);
$Image_Pay= ($image_counter/1000)*10;
update_user_meta($id, $monthly_Image_Pay, $Image_Pay);
$Comment_Pay= ($comment_counter/1000)*10;
update_user_meta($id, $monthly_Comment_Pay, $Comment_Pay);
$Total_Payment= $View_Pay + $Word_Pay + $Image_Pay+ $Comment_Pay;
update_user_meta($id, $monthly_Total_Pay, $Total_Payment);
$counter = 0;
$word_counter = 0;
$image_counter = 0;
$comment_counter = 0;
}
}
add_action('Milyin_Daily', 'Milyin_Admin_Mail');
Here's the code for the cronjob i wrote for daily, but as soon as i replace it with the word monthly things break
function Daily_Counter_Cron() {
if( !wp_next_scheduled( 'Milyin_Daily' ) ) {
wp_schedule_event( strtotime('00:00:00'), 'daily', 'Daily_Counter' ); }
}
add_action('wp', 'Daily_Counter_Cron');
I have been trying to run a function at first of every month, and first of every year. And i wanted to run at 12am exact. Below I have shown the function. When is schedule it for daily it works perfectly, but if I try to run it monthly or yearly, it breaks.
I feel that it can be that there is no facility to run yearly. Nevertheless below is the code, please help me run it at start of each month and each year
function Milyin_Admin_Mail(){ $authors=get_users(); foreach($authors as $author){
$author_posts = get_posts( array('author' => $author->id, 'numberposts' => -1 ));
// needed to collect the total sum of views
$counter = 0;
$word_count=0;
$image_count=0;
$comment_count=0;
$monthly_Views= date("m-Y") . '-Views';
$monthly_Words=date("m-Y") .'-Words';
$monthly_Images=date("m-Y") .'-Images';
$monthly_Comments=date("m-Y") .'-Comments';
$monthly_View_Pay=date("m-Y") .'-View-Pay';
$monthly_Word_Pay=date("m-Y") .'-Word-Pay';
$monthly_Image_Pay=date("m-Y") .'-Image-Pay';
$monthly_Comment_Pay=date("m-Y") .'-Comment-Pay';
$monthly_Total_Pay=date("m-Y") .'-Total-Pay';
// do stuff to get author name
foreach ( $author_posts as $post ) {
$views = absint( get_post_meta( $post->ID, 'Creation_Views', true ) );
$word_count = str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
$image_count = count( get_attached_media( 'image', $post->ID ) );
$comment_count = get_comments_number($post->ID) ;
$counter += $views;
$word_counter += $word_count;
$image_counter += $image_count;
$comment_counter += $comment_count;
}
$id= $author->id;
update_user_meta($id, $monthly_Views, $counter);
update_user_meta($id, $monthly_Words, $word_counter);
update_user_meta($id, $monthly_Images, $image_counter);
update_user_meta($id, $monthly_Comments, $comment_counter);
$View_Pay = ($counter/1000)*400;
update_user_meta($id, $monthly_View_Pay, $View_Pay);
$Word_Pay= ($word_counter/1000)*10;
update_user_meta($id, $monthly_Word_Pay, $Word_Pay);
$Image_Pay= ($image_counter/1000)*10;
update_user_meta($id, $monthly_Image_Pay, $Image_Pay);
$Comment_Pay= ($comment_counter/1000)*10;
update_user_meta($id, $monthly_Comment_Pay, $Comment_Pay);
$Total_Payment= $View_Pay + $Word_Pay + $Image_Pay+ $Comment_Pay;
update_user_meta($id, $monthly_Total_Pay, $Total_Payment);
$counter = 0;
$word_counter = 0;
$image_counter = 0;
$comment_counter = 0;
}
}
add_action('Milyin_Daily', 'Milyin_Admin_Mail');
Here's the code for the cronjob i wrote for daily, but as soon as i replace it with the word monthly things break
function Daily_Counter_Cron() {
if( !wp_next_scheduled( 'Milyin_Daily' ) ) {
wp_schedule_event( strtotime('00:00:00'), 'daily', 'Daily_Counter' ); }
}
add_action('wp', 'Daily_Counter_Cron');
Share
Improve this question
edited Sep 16, 2019 at 8:33
Aditya Agarwal
asked Sep 16, 2019 at 5:05
Aditya AgarwalAditya Agarwal
2764 silver badges22 bronze badges
1 Answer
Reset to default 1If you need it on the first day of the month, you should still run this daily. Just be sure to check if "today" is the first of the month. Even your "first day of the year" would be executed if it is the first day of the month.
You can check this with the following:
$dtFirst = new DateTime('first day of this month');
$dtToday = new DateTime('today');
$interval = $dtToday->diff($dtFirst);
// format('%a') produces total 'days' from DateTime::diff()
if ($interval->format('%a') == '0'){
// Do your stuff here...
}
Doing this check daily will avoid issues with leap years, etc., without significant impact to your server.
I would also recommend you use a system cron to replace the WP Cron, to ensure you get a proper execution. WP Cron is too dependant on user activity.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745153849a4613996.html
评论列表(0条)