如何从WordPress‘WP Crontrol’插件运行SQL查询

时间:2020-11-08 作者:chewflow

我有一个SQL查询,如果我直接在数据库中运行它,它可以实现我想要的功能:

UPDATE wp_pmpro_memberships_users
SET wp_pmpro_memberships_users.status = \'admin_cancelled\'
WHERE wp_pmpro_memberships_users.user_id IN 
    (
    /*Select all users that have an active Membership but no active Subscription*/
    SELECT pmpro.user_id FROM wp_pmpro_memberships_users as pmpro
    JOIN wp_pmpro_membership_levels as pmprol ON pmprol.id=pmpro.membership_id
    JOIN wp_users ON pmpro.user_id = wp_users.ID
    WHERE pmpro.status = \'active\'
    AND pmpro.user_id NOT IN 
        (
        /*Select all users that have an active Subscription*/
        SELECT meta.meta_value 
        FROM wp_posts as p
        JOIN wp_postmeta as meta
        ON meta.post_id = p.id
        JOIN wp_users as u
        ON meta.meta_value = u.id
        WHERE post_type LIKE \'shop_subscription\'
        AND post_status LIKE \'wc-active\'
        AND meta.meta_key = \'_customer_user\'
        )
    )
并将此代码添加到我的函数中。“我的孩子”主题中的php文件:

add_action("cron_expire_nonactive_memberships", "cron_expire_nonactive_memberships");
function cron_expire_nonactive_memberships() {
    global $wpdb;
    $sqlQuery = "UPDATE {$wpdb->wp_pmpro_memberships_users}
    SET wp_pmpro_memberships_users.status = \'admin_cancelled\'
    WHERE wp_pmpro_memberships_users.user_id IN 
    (
    /*Select all users that have an active Membership but no active Subscription*/
    SELECT pmpro.user_id FROM {$wpdb->wp_pmpro_memberships_users} as pmpro
    JOIN {$wpdb->wp_pmpro_membership_levels} as pmprol ON pmprol.id=pmpro.membership_id
    JOIN {$wpdb->wp_users} ON pmpro.user_id = wp_users.ID
    WHERE pmpro.status = \'active\'
    AND pmpro.user_id NOT IN 
        (
        /*Select all users that have an active Subscription*/
        SELECT meta.meta_value 
        FROM {$wpdb->wp_posts} as p
        JOIN {$wpdb->wp_postmeta} as meta
        ON meta.post_id = p.id
        JOIN {$wpdb->wp_users} as u
        ON meta.meta_value = u.id
        WHERE post_type LIKE \'shop_subscription\'
        AND post_status LIKE \'wc-active\'
        AND meta.meta_key = \'_customer_user\'
        )
    )";
}
但我正在尝试使用一个名为“WP Crontrol”的插件来运行它。

它有一个添加新Cron事件的选项,因此我使用了钩子cron_expire_nonactive_memberships 并且一直在手动运行,但什么都没有发生。

如果这有什么不同的话,我会以本地主机的身份运行。

我不确定尝试运行SQL的php代码是否有问题-如何调试发生的情况?

Edit:

嗯,我可以在错误日志中看到以下内容,但我不确定如何解决:

PHP Notice: Undefined property: wpdb::$wp_users in C:\\xampp\\htdocs\\myproject\\wp-includes\\wp-db.php on line 648

1 个回复
SO网友:chewflow

我通过使用以下工具使事情正常运行:

include_once("wp-config.php");
include_once("wp-includes/wp-db.php");

global $wpdb;

$sql = "UPDATE wp_pmpro_memberships_users
SET wp_pmpro_memberships_users.status = \'admin_cancelled\'
WHERE wp_pmpro_memberships_users.user_id IN 
    (
    /*Select all users that have an active Membership but no active Subscription*/
    SELECT pmpro.user_id FROM wp_pmpro_memberships_users as pmpro
    JOIN wp_pmpro_membership_levels as pmprol ON pmprol.id=pmpro.membership_id
    JOIN wp_users ON pmpro.user_id = wp_users.ID
    WHERE pmpro.status = \'active\'
    AND pmpro.user_id NOT IN 
        (
        /*Select all users that have an active Subscription*/
        SELECT meta.meta_value 
        FROM wp_posts as p
        JOIN wp_postmeta as meta
        ON meta.post_id = p.id
        JOIN wp_users as u
        ON meta.meta_value = u.id
        WHERE post_type LIKE \'shop_subscription\'
        AND post_status LIKE \'wc-active\'
        AND meta.meta_key = \'_customer_user\'
        )
    )";
    
$results = $wpdb->get_results($sql);

相关推荐

WP cron、管理员和更新问题

我正在构建一个cron,它可以进行备份、更新插件、更新核心、在发生更新时向我发送电子邮件,或者在没有更新时删除备份。cron已经安排在一个周末的晚上,让我有机会浏览这个网站,看看是否有什么东西坏了。我在一个专用于开发人员的自定义管理页面上构建并测试了cron中运行的钩子/函数的代码。睡前我突然想到,cron中的条件不一样,它不会有一个登录的管理员,也不会加载与管理页面相同的文件。我错了吗,wp cron在运行任务之前需要管理文件吗?用户是否需要登录插件或core才能更新?