我正在尝试以下方法:
当产品页面被永久删除时,它还应该删除: 产品图库和产品图片附在帖子上。
我尝试了我发现的解决方案: https://wordpress.stackexchange.com/questions/109793/delete-associated-media-upon-page-deletion
但这对我没用。
答案 0 :(得分:2)
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;
}
答案 1 :(得分:2)
虽然这是一个老问题,但我会留在这里以防万一将来会有人在寻找它。
由于您要删除产品图片,您可以尝试使用这些简洁便捷的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 );
}
}
}
对我来说很好。
答案 2 :(得分:0)
您可以尝试以下方法:
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);
}
}
}
当产品被永久删除时,这将删除图像。
答案 3 :(得分:0)
Step 1: Login to Your PhpMyAdmin | If you have more than one database, make sure you click on the correct database. If you’re not sure because you have more than one WordPress installation on the same domain, open a table and look for “wp_options”, you’ll be able to see the URL of the website you’re making changes to.
Step 2: Run the SQL Statement ==> in WP_POST
If you are on the correct database, click on the SQL tab on the top right. It might be empty or have an SQL statement there from a previous command.
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';
答案 4 :(得分:0)
尝试一次,不需要更多代码即可删除产品。 您可以将此代码添加到一个新文件中,然后放置在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);