我正在尝试设置一个cron作业来删除超过48小时的行。我以前从未这样做过,并决定尝试制作WP预定任务。以下是我的functions.php
文件中的代码。我假设删除行/查询错误。任何帮助,将不胜感激;或者,如果有一种更容易的方式,我也会对此持开放态度。感谢。
add_action('init','es4u_delete_old_videos');
add_action('es4u_delete_video','es4u_delete_video_func');
function es4u_delete_video_func() {
$servername = "localhost";
$username = "username";
$password = "pass";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
DELETE * FROM awdwp_webinar_orders WHERE start_time < DATEADD(HOUR,-48,GETDATE());
}
function es4u_delete_old_videos(){
if(!wp_next_scheduled('es4u_delete_video')) {
wp_schedule_event (time(), 'hourly', 'es4u_delete_video');
}
}
编辑:这就是我现在所拥有的,但表格仍未删除。有什么想法吗?
add_action('init','es4u_delete_old_videos');
add_action('es4u_delete_video','es4u_delete_video_func');
function es4u_delete_video_func() {
global $wpdb;
$sql = 'DELETE * FROM awdwp_webinar_orders WHERE start_time < DATEADD(HOUR,-48,GETDATE());';
$wpdb->query( $sql );
}
function es4u_delete_old_videos(){
if(!wp_next_scheduled('es4u_delete_video')) {
wp_schedule_event (time(), 'hourly', 'es4u_delete_video');
}
}
答案 0 :(得分:2)
您可以使用内置的WordPress类wpdb代替使用mysqli
函数来为您完成一些工作。 WordPress创建了一个可以使用的全局$wpdb
对象。
function es4u_delete_video_func() {
global $wpdb;
$sql = 'DELETE FROM awdwp_webinar_orders WHERE start_time < DATE_ADD( NOW(), INTERVAL -48 HOUR )';
$wpdb->query( $sql );
}