我一直试图在每个月初和每年初运行一个函数。我想在凌晨12点跑步。下面我展示了这个函数。当我把它安排在每天的时候,它工作得很好,但如果我试着每月或每年运行一次,它就坏了。
我觉得可能是因为没有每年运行的设施。尽管如此,下面是代码,请帮助我在每个月和每年年初运行它
函数Milyin\\u Admin\\u Mail(){$authors=get\\u users();foreach($authors为$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\');
这是我为《每日邮报》编写的cronjob的代码,但一旦我将其替换为单词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\');