حذف تصویر محصول هنگام حذف محصول ووکامرسی

amraja

تازه وارد
آیا روشی یا کدی یا افزونه ای هست که هنگام انتقال محصول به زباله دان عکس و گالری های محصول هم حذف شوند؟

 

Mohammad

مدیر انجمن
پرسنل مدیریت
ازمایش نکردم

اینو تست کنید

فایل فانکشن قالب رو باز کنید و این رو در انتهاش قرار بدید

function o99_delete_post_children($post_id) {
global $wpdb;

$child_atts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'");

foreach ( $child_atts as $id )
wp_delete_attachment($id);
}
add_action('before_delete_post', ' o99_delete_post_children');
add_action('trash_post', 'o99_delete_post_children')




یک کد دیگه:

کد:
add_action('before_delete_post', 'delete_post_attachments');
function delete_post_attachments($post_id){

    global $post_type;   
    if($post_type !== 'product') return;

    global $wpdb;

    $args = array(
        'post_type'         => 'attachment',
        'post_status'       => 'any',
        'posts_per_page'    => -1,
        'post_parent'       => $post_id
    );
    $attachments = new WP_Query($args);
    $attachment_ids = array();
    if($attachments->have_posts()) : while($attachments->have_posts()) : $attachments->the_post();
            $attachment_ids[] = get_the_id();
        endwhile;
    endif;
    wp_reset_postdata();

    if(!empty($attachment_ids)) :
        $delete_attachments_query = $wpdb->prepare('DELETE FROM %1$s WHERE %1$s.ID IN (%2$s)', $wpdb->posts, join(',', $attachment_ids));
        $wpdb->query($delete_attachments_query);
    endif;
 
آخرین ویرایش توسط مدیر:
بالا