1

can you help me with this please, what am I missing?

This one works:

if ( ! wp_next_scheduled( 'my_hook_2' ) ) {
  wp_schedule_event( time(), 'hourly', 'my_hook_2' );
}

add_action( 'my_hook_2', 'my_task_function' );

function my_task_function() {
  wp_mail( 'mymail@gmail.com', 'Automatic email', 'Automatic scheduled email from WordPress.');
}

This one does not work:

if ( ! wp_next_scheduled( 'my_hook_3' ) ) {
  wp_schedule_event( time(), 'hourly', 'my_hook_3' );
}

add_action( 'my_hook_3', 'my_task_function' );

function my_task_function() {
    $sql_logs = "SELECT * FROM wp_wtd_search_log WHERE year(Date) = year(CURDATE()) AND day(Date) = day(CURDATE())";
    $get_logs = $wpdb->get_results( $sql_logs );        

    $message = '<html><body><table border="5" style="border-style:none;">';

    foreach ( $get_logs as $rows){    
    $message .= '<tr><td>'.$rows->IP.'</td><td>'.$rows->Keyword.'</td><td>'.$rows->Date.'</td></tr>';
    }
    $message .= '</table></body></html>';

    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    mail("mymail@gmail.com","mytitle Search Logs",$message,$headers);
}

Edit for comments:

If you think this caused because of my query, I created a file with my query as below, named it as report.php. Then manually visited this file and received an e-mail, which I believe proves that my query works fine.

Report.php:

<?php
$inc = '../../../../wp-load.php';

if (file_exists($inc) && is_readable($inc)) {
    require_once($inc);
} else {
    require_once('wp-load.php'); 
}

$sql_logs = "SELECT * FROM wp_wtd_search_log WHERE year(Date) = year(CURDATE()) AND day(Date) = day(CURDATE())";
$get_logs = $wpdb->get_results( $sql_logs );
$message = '<html><body><table border="5" style="border-style:none;">';
foreach ( $get_logs as $rows){    
$message .= '<tr><td>'.$rows->IP.'</td><td>'.$rows->Keyword.'</td><td>'.$rows->Date.'</td></tr>';       
}
$message .= '</table></body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

mail("mymail@gmail.com","mysite Search Logs",$message,$headers);

?>
  • Hi. What do you mean by Not Work? Both of your codes run on cron in the same way. So I don't think it's about wp-cron. It's possibly about your query. – Jack Johansson Jun 25 '17 at 23:05
  • 2
    I strongly suspect this has nothing to do with cron, and a misunderstanding of how to use global variables, global $wpdb; may be all you need, and you're likely getting errors in your PHP error log when this runs – Tom J Nowell Jun 25 '17 at 23:43
  • @JackJohansson What I mean is, I receive a mail from the first one, and I don't receive an e-mail from the second one. They are both inside my functions.php. I have written my query into a .php file and manually visited this file and received a mail which proved to me that my query works fine. – HOY Jun 27 '17 at 11:28
  • @TomJNowell There is no error log file created afterwards, so I don't see any errors. To check if there is some mistake in my query, I have written my query into a .php file and manually visited this file and received a mail which proved to me that my query works fine. – HOY Jun 27 '17 at 11:30
  • An you're sure your cron job actually runs? And that error logging is turned on? – Tom J Nowell Jun 27 '17 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.