0

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...

1 Answer 1

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.

5
  • And how can i do that? Should i include wp-config and blog-header files in it to make it run? Commented Sep 20, 2019 at 7:16
  • 1
    no. try this path to wpload file. require_once("../../../../wp-load.php"); Commented Sep 20, 2019 at 7:19
  • Will try and let you know in 2 minutes Commented Sep 20, 2019 at 7:20
  • I included the wp-load.php and moved my file itself to root directory for simplicity. The cron runs at 3 mins from now, let's hope it works... Commented Sep 20, 2019 at 7:27
  • 1
    Hey there, Super Happy to inform you that it worked, included these 3 lines into code. require_once('wp-blog-header.php'); require_once('wp-config.php'); require_once('wp-load.php'); And it did the trick. Please include this in your answer, so that i can mark it as the correct solution. Thanks a lot for this help... Commented Sep 20, 2019 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.