Wordpress 在删除产品时,还会自动删除产品库和产品图像

Wordpress 在删除产品时,还会自动删除产品库和产品图像,wordpress,woocommerce,Wordpress,Woocommerce,我尝试了以下方法: 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_ima

我尝试了以下方法:

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);
    }
  }
}
当产品页面被删除(永久删除)时,还应删除: 产品图库和产品图片附在帖子上

我尝试了在以下网站上找到的解决方案:


但这对我不起作用。

您可以尝试以下方法:

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);
    }
  }
}

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

虽然这是一个老问题,但我将把它留在这里,以防将来有人会寻找它

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;
}
由于您想要删除产品图像,您可以尝试使用这些简洁方便的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 );
        }
    }
}

对我来说效果很好。

步骤1:登录您的PhpMyAdmin;如果您有多个数据库,请确保单击正确的数据库。如果您不确定,因为您在同一个域上安装了多个WordPress,请打开一个表并查找“wp_选项”,您将能够看到您正在更改的网站的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';
再试一次,删除产品不需要更多代码。 您可以将此代码添加到一个新文件中,并放置在wordpress的根目录中。

include('wp-load.php');
功能删除职位($sku)
{
全球$wpdb;
$result_post=$wpdb->get_results(“从`wp_posts`中选择`ID',其中`post_name`像CONCAT('%',“$sku.”,'%'));
对于($q=0;$qID,true);//wp\u delete\u发布完整产品删除及其所有图像
}
}
删除职位($sku);

他指定他只想将此用于产品,因此我添加了一个检查产品帖子类型的复选框。这对我还不起作用,我查看了数据库并将“\u缩略图\u id”替换为“\u wp\u附件”。你能告诉我$key或“key”中应该包含什么吗谢谢如果你以代码的形式给出答案,试着解释它,而不是像这样给出代码。我从未来来到这里,想说声谢谢!!在
wp includes/functions.php
中添加此代码成功了!!不要做@Mahmoud Eidarous做的事。永远不要编辑核心WordPress文件。。你至少可以提出一个更好的答案。@Mahmoudeidarus你是对的。不要照马哈茂德的建议去做。永远不要编辑核心WordPress文件。如果要添加自定义代码,请将其添加到自定义主题、子主题或自定义插件中。