Trying to add a cron job on Cpanel to run every 5 minutes. I added this to the command input (removed personal details from the url)
/usr/bin/php -q /home/my_name/public_html/staging/the_web_site/wp-content/themes/my_child_theme/functions.php updateproducts
Then on the functions.php
, I have this:
if (!empty($argv[1])) {
switch ($argv[1]) {
case "updateproducts":
update_products();
break;
}
}
The update_products()
function runs without any error manually if I trigger it with a button on admin page. But, no matter what I do on the cronjob tab, it doesn't run.
Any idea?
function update_products() {
global $wpdb;
$groups = get_groups_from_cron_jobs(100);
foreach ( $groups as $group ) {
$name = $group->group_name;
$sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}products WHERE name='%s'", $name);
$products = $wpdb->get_results($sql);
if ( !empty($products) ){
$post_id = get_post_id_from_products($products);
//if there isn't a parent_id then create a new product
if ( !$post_id && $name != '' ) {
$post_id = create_a_new_product($name);
}
// make sure that all products will have now a parent_id
add_parent_id_on_products($name, $post_id);
insert_product_attributes($post_id, $products);
insert_product_variations($post_id, $products);
delete_group_from_cron_jobs($name);
}
}
}
Edit: Based on the answers/comments, I made extra research and found that I can load the $wpdb on any script. What I did is this:
if (!empty($argv[1])) {
switch ($argv[1]) {
case "updateproducts":
$path = $_SERVER['DOCUMENT_ROOT'];
require( $path . '/staging/the_web_site/wp-load.php' );
update_products();
break;
}
}
However, I still get the error on email:
Status: 500 Internal Server Error
X-Powered-By: PHP/5.6.31
Content-type: text/html; charset=UTF-8
I'm afraid there's some misunderstanding about how inter-operable the web environment and console environment are here.
In short, your function expects WordPress to exist. It expects that the user has accessed the function through the WordPress application, and that global variables such as $wpdb
exist (among other things). If you access this function via the admin page, then this is all true, and the function works.
But if you access this function via the console (command line), none of this is true. WordPress does not exist within the context of that request. global $wpdb
does not exist, because the function has not been accessed through WordPress, but through a direct command.
If you want this function to work on the command line, you will need to rewrite it to work within that environment. Which means no WordPress-specific helpers or functionalities at all.
Alternatively, you can use WordPress Crons
, which are not true Cron jobs, but instead run whenever the site is accessed (within their defined time range). So if you have a cron that must run every five minutes, but you will not reliably have a new visitor every few minutes, these will not suit your needs, but they are an option.
http://www.wpbeginner.com/plugins/how-to-view-and-control-wordpress-cron-jobs/