wordpress Woocommerce删除产品时,也会自动删除产品图库和产品图片

slwdgvem  于 2023-02-03  发布在  WordPress
关注(0)|答案(6)|浏览(249)

我尝试了以下方法:
当一个产品页面被删除(永久),它也应该删除:产品图库和产品图像,它们附在帖子上。
我尝试了解决方案,我发现:https://wordpress.stackexchange.com/questions/109793/delete-associated-media-upon-page-deletion
但我没成功。

0qx6xfy6

0qx6xfy61#

虽然这是一个老问题,但我将把它留在这里,以防将来有人会寻找它。
既然你想删除产品图片,你可以试试这些简洁方便的woocommerce功能来获取附件id。
大概是这样的

add_action( 'before_delete_post', 'delete_product_images', 10, 1 );

function delete_product_images( $post_id )
{
    $product = wc_get_product( $post_id );

    if ( !$product ) {
        return;
    }

    $featured_image_id = $product->get_image_id();
    $image_galleries_id = $product->get_gallery_image_ids();

    if( !empty( $featured_image_id ) ) {
        wp_delete_post( $featured_image_id );
    }

    if( !empty( $image_galleries_id ) ) {
        foreach( $image_galleries_id as $single_image_id ) {
            wp_delete_post( $single_image_id );
        }
    }
}

对我来说很好。

vawmfj5a

vawmfj5a2#

add_action("before_delete_post","wdm_delete_post_images",10,1);

function wdm_delete_post_images($post_id)
{
 global $wpdb;
          $args = array(
                'post_parent' => $post_id,
                'post_type'   => 'attachment', 
                'numberposts' => -1,
                'post_status' => 'any' 
        ); 
        $childrens = get_children( $args);
        if($childrens):
            foreach($childrens as $attachment):   
             wp_delete_attachment( $attachment->ID, true ); 
             $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = ".$attachment->ID);
             wp_delete_post($attachment->ID,true ); 
            endforeach;

        endif;
}
iqxoj9l9

iqxoj9l93#

您可以尝试以下操作:

add_action('after_delete_post','wdm_delete_post_images',10,1);

function wdm_delete_post_images($post_id)
{
   $featured_image_id = get_post_meta($post_id,'_thumbnail_id',true);

   $image_galleries_id = get_post_meta($post_id,'_product_image_gallery',true);

   if(!empty($featured_image_id))
   {
     wp_delete_post($featured_image_id);
   }

  if(!empty($image_galleries_id))
  {
    $image_galleries_array = split(',',$image_galleries_id);

    foreach($image_galleries_array as $single_image_id)
    {
        wp_delete_post($single_image_id);
    }
  }
}

这将在产品被永久删除时删除图像。

ippsafx7

ippsafx74#

步骤1:登录到您的PhpMyAdmin|如果您有多个数据库,请确保您点击了正确的数据库。如果您因为在同一个域中有多个WordPress安装示例而不确定,请打开一个表并查找“wp_options”,您将能够看到您正在更改的网站的URL。
步骤2:在WP_POST中运行SQL语句==〉
如果你在正确的数据库上,点击右上角的SQL选项卡。它可能是空的,或者有一个来自上一个命令的SQL语句。

DELETE relations.*, taxes.*, terms.*
FROM wp_term_relationships AS relations
INNER JOIN wp_term_taxonomy AS taxes
ON relations.term_taxonomy_id=taxes.term_taxonomy_id
INNER JOIN wp_terms AS terms
ON taxes.term_id=terms.term_id
WHERE object_id IN (SELECT ID FROM wp_posts WHERE post_type='product');

DELETE FROM wp_postmeta WHERE post_id IN (SELECT ID FROM wp_posts WHERE post_type = 'product');
DELETE FROM wp_posts WHERE post_type = 'product';
6gpjuf90

6gpjuf905#

**尝试一次,不需要任何更多的代码删除产品.**您可以将此代码添加到一个新文件中,并放置在WordPress的根目录下.

include('wp-load.php');
    function delete_post($sku)
    {
        global $wpdb;
        $result_post = $wpdb->get_results ( "SELECT `ID` FROM `wp_posts` WHERE `post_name` LIKE CONCAT('%',".$sku.", '%')" );
            for($q=0;$q<count($result_post);$q++)
            {
                wp_delete_post($result_post[$q]->ID,true); // wp_delete_post full product delete with its all images
            }
    }

    delete_post($sku);
x7rlezfr

x7rlezfr6#

我用这个代码,它的作品.但我试图添加更多的代码来创建一个函数,当我删除一个产品明确,这除了被从WordPress的“媒体”文件夹中删除,是从我的FTP中的另一个文件夹中删除,在那里相同的图像存在相同的名称.所以我只需要知道文件的名称,从“media”文件夹中删除它,然后从另一个有该文件副本的文件夹中删除它。我该怎么做?

add_action( 'before_delete_post', 'delete_product_images', 10, 1 );

function delete_product_images( $post_id )
{
$product = wc_get_product( $post_id );

if ( !$product ) {
    return;
}

$featured_image_id = $product->get_image_id();
$image_galleries_id = $product->get_gallery_image_ids();

if( !empty( $featured_image_id ) ) {
    wp_delete_post( $featured_image_id );
}

if( !empty( $image_galleries_id ) ) {
    foreach( $image_galleries_id as $single_image_id ) {
        wp_delete_post( $single_image_id );
    }
}

} }

相关问题