plugins - get_users(); Is an Undefined Function When Used in Cron Job

I am Using a Wordpress cron to update some usermeta of each user every 24 hours. For which i created a cronjob using the

I am Using a Wordpress cron to update some usermeta of each user every 24 hours. For which i created a cronjob using the interface provided by my hosting provider.

The Cron runs, but the code cannot be executed due to a Fatal Error Call to undefined function get_users() I have used this function without much knowledge at many other places, how can I ensure that it works here also. I only need to get the used id, and loop each user id, and then loop through each post of each user id.

The code. The issue is on line number 3 :

<?php

$authors=get_users(array( 'fields' => array( 'ID' ) ) );

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; 
$word_count=0;
$image_count=0;
$comment_count=0;

$daily_Views= date("d-m-Y") . '-Views';
$daily_Words=date("d-m-Y") .'-Words'; 
$daily_Images=date("d-m-Y") .'-Images';
$daily_Comments=date("d-m-Y") .'-Comments';
$daily_View_Pay=date("d-m-Y") .'-View-Pay';
$daily_Word_Pay=date("d-m-Y") .'-Word-Pay';
$daily_Image_Pay=date("d-m-Y") .'-Image-Pay';
$daily_Comment_Pay=date("d-m-Y") .'-Comment-Pay';
$daily_Total_Pay=date("d-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, $daily_Views, $counter);
update_user_meta($id, $daily_Words, $word_counter);
update_user_meta($id, $daily_Images, $image_counter);
update_user_meta($id, $daily_Comments, $comment_counter);
$View_Pay = ($counter/1000)*400;
update_user_meta($id, $daily_View_Pay, $View_Pay);
$Word_Pay= ($word_counter/1000)*10;
update_user_meta($id, $daily_Word_Pay, $Word_Pay);
$Image_Pay= ($image_counter/1000)*10;
update_user_meta($id, $daily_Image_Pay, $Image_Pay);
$Comment_Pay= ($comment_counter/1000)*10;
update_user_meta($id, $daily_Comment_Pay, $Comment_Pay);
$Total_Payment= $View_Pay + $Word_Pay + $Image_Pay+ $Comment_Pay;
update_user_meta($id, $daily_Total_Pay, $Total_Payment);
$counter = 0;
$word_counter = 0;
$image_counter = 0;
$comment_counter = 0;
}
?>

It seems to be perfect, infact i tried some sites to check my code, and by syntax it is surely correct, how can i ensure that on line 3, my code gets all the users, and later in the loop we get user id for each of the user...

I am Using a Wordpress cron to update some usermeta of each user every 24 hours. For which i created a cronjob using the interface provided by my hosting provider.

The Cron runs, but the code cannot be executed due to a Fatal Error Call to undefined function get_users() I have used this function without much knowledge at many other places, how can I ensure that it works here also. I only need to get the used id, and loop each user id, and then loop through each post of each user id.

The code. The issue is on line number 3 :

<?php

$authors=get_users(array( 'fields' => array( 'ID' ) ) );

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; 
$word_count=0;
$image_count=0;
$comment_count=0;

$daily_Views= date("d-m-Y") . '-Views';
$daily_Words=date("d-m-Y") .'-Words'; 
$daily_Images=date("d-m-Y") .'-Images';
$daily_Comments=date("d-m-Y") .'-Comments';
$daily_View_Pay=date("d-m-Y") .'-View-Pay';
$daily_Word_Pay=date("d-m-Y") .'-Word-Pay';
$daily_Image_Pay=date("d-m-Y") .'-Image-Pay';
$daily_Comment_Pay=date("d-m-Y") .'-Comment-Pay';
$daily_Total_Pay=date("d-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, $daily_Views, $counter);
update_user_meta($id, $daily_Words, $word_counter);
update_user_meta($id, $daily_Images, $image_counter);
update_user_meta($id, $daily_Comments, $comment_counter);
$View_Pay = ($counter/1000)*400;
update_user_meta($id, $daily_View_Pay, $View_Pay);
$Word_Pay= ($word_counter/1000)*10;
update_user_meta($id, $daily_Word_Pay, $Word_Pay);
$Image_Pay= ($image_counter/1000)*10;
update_user_meta($id, $daily_Image_Pay, $Image_Pay);
$Comment_Pay= ($comment_counter/1000)*10;
update_user_meta($id, $daily_Comment_Pay, $Comment_Pay);
$Total_Payment= $View_Pay + $Word_Pay + $Image_Pay+ $Comment_Pay;
update_user_meta($id, $daily_Total_Pay, $Total_Payment);
$counter = 0;
$word_counter = 0;
$image_counter = 0;
$comment_counter = 0;
}
?>

It seems to be perfect, infact i tried some sites to check my code, and by syntax it is surely correct, how can i ensure that on line 3, my code gets all the users, and later in the loop we get user id for each of the user...

Share Improve this question asked Sep 20, 2019 at 6:18 Aditya AgarwalAditya Agarwal 2764 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you are running a cron file in crontab then. including WP files will not help as it doesnot run as a wordpress file. it is like a simple php script. You will not get access to access wordpress functions. you will have to run php script and connect using php connection to the database.

Some servers which host wordpress and support will execute. for them you will have to provide path to the files of wp-config and includes form the root level.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745144241a4613574.html

相关推荐

  • plugins - get_users(); Is an Undefined Function When Used in Cron Job

    I am Using a Wordpress cron to update some usermeta of each user every 24 hours. For which i created a cronjob using the

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信