in WordPress Loop update_user_ meta with Current Month and Year as Meta_Ley

I am making a page for my wordpress authors to see there stats. For the same, i first loop through all author ids, then i loop through all of there posts, and sum the post views meta key for all. I have 3 cron jobs that store the views monthly, daily, and lifetime.

Given below is code for same. The given code for for the monthly cron job, it sums the Views post meta for all posts of an author, and then assign the sum a variable $counter. After that at end of each month, i use this cronjob to store the sum as a user_meta, so that it can be used in my statistics and be retrieved at various places in my site.

As u can see this code has a meta key August_2019_Views, and many similar other keys of format Month_Year_XYZ

The current code as you can see works, but i manually have to change the month and the year, periodically which is a headache

function Milyin_Admin_Mail(){
$authors=get_users();
foreach($authors as $author){
// do stuff to get user I
$author_posts = get_posts( array(‘author’ => $author->id, ‘numberposts’ => -1 ));
// needed to collect the total sum of views
$counter = 0;
// do stuff to get author name
foreach ( $author_posts as $post ) {
$views = absint( get_post_meta( $post->ID, ‘Creation_Views’, true ) );
$counter += $views;
}
$id= $author->id;
update_user_meta($id, ‘August_2019_Views’, $counter);
$View_Pay = ($counter/1000)*400;
update_user_meta($id, ‘August_2019_View_Pay’, $View_Pay);
}
}
add_action(‘mycronjob’, ‘Milyin_Admin_Mail’);

function cronstarter_activation() {
if( !wp_next_scheduled( ‘mycronjob’ ) ) {
wp_schedule_event( time(), ‘monthly’, ‘mycronjob’ ); }
}
add_action(‘wp’, ‘cronstarter_activation’);

And it works, but i need to change month at dozens of places besides this code also. Please help me assign a variable of thar format.

$format= date(‘M_Y’);
$format= _Views;
update_user_meta($id, ‘$format’, $counter);
$Total = get_user_meta($author_id, ‘$format’, true);

Read more here:: in WordPress Loop update_user_ meta with Current Month and Year as Meta_Ley

Leave a Reply

Your email address will not be published. Required fields are marked *