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