繁体   English   中英

运行 SQL 查询将所有已发布的 WooCommerce 产品更改为草稿状态

[英]Run a SQL Query to change all published WooCommerce product to draft status

我正在尝试通过我的functions.php运行SQL查询。php 文件,一旦工作,将其移动到插件中,这样,通过激活它,查询将运行。 然后我可以通过激活/停用插件来运行查询。

目标是将所有状态为published的产品更改为drafts 这是我得到的,这是行不通的。

这里有人可以帮忙吗?

add_action('wp_head','turn_all_products_into_drafts');
function turn_all_products_into_drafts(){
    include_once("wp-config.php");
    include_once("wp-includes/wp-db.php");
        global $wpdb;
        $sql = "UPDATE wp_posts SET post_status = 'draft' WHERE post_type = 'product' AND post_status = 'publish'";
                    $wpdb->update($sql);
        }

我认为您不需要:

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

因为这是一个 Wordpress 插件,并且在加载插件时这两个插件已经可用。

此外,您应该以这种方式更好地使用 init 钩子和WPDB query()方法

add_action( 'init', 'turn_all_products_into_drafts');
function turn_all_products_into_drafts(){
    global $wpdb; 

    $wpdb->query( "
        UPDATE {$wpdb->prefix}posts
        SET post_status = 'draft'
        WHERE post_type = 'product'
        AND post_status = 'publish'
    " );
}

它应该更好地工作。

会不会是你有拼写错误? 在描述中你说:

状态已发布

然后在 select 你有:

post_status = '发布'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM