Skip to content

Commit

Permalink
added media delete in custom query
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat1192 committed Jan 19, 2024
1 parent ec160a2 commit f27f24c
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 179 deletions.
29 changes: 19 additions & 10 deletions includes/class-delete-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,29 @@ public function do_delete_posts( $post_ids = array(), $force_delete = false, $it
}

if( ! empty( $post_ids ) && count( $post_ids ) > 0 ) {

$post_attechment_id = get_post_meta( $post_id, '_thumbnail_id', true );
$attechment_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = $post_attechment_id" );
if( isset( $item['post_media'] ) && $item['post_media'] === 'yes' ){
if( count( $attechment_ids ) <= 1 ){
wp_delete_attachment( $post_attechment_id, $force_delete );
}
}


if( $custom_query == 'custom_query' ){

foreach( $post_ids as $post_id ){
$post_attechment_id = get_post_meta( $post_id, '_thumbnail_id', true );
$attechment_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = $post_attechment_id" );
if( isset( $item['post_media'] ) && $item['post_media'] === 'yes' ){
if( count( $attechment_ids ) <= 1 ){
wp_delete_attachment( $post_attechment_id, $force_delete );
}
}
}
$all_posts = implode( ",",$post_ids );
$wpdb->query( "DELETE p,pt,pm FROM " . $wpdb->posts . " p LEFT JOIN " . $wpdb->term_relationships . " pt ON pt.object_id = p.ID LEFT JOIN " . $wpdb->postmeta . " pm ON pm.post_id = p.ID WHERE p.ID IN ({$all_posts})" );
}else{
foreach ($post_ids as $post_id ) {
foreach ($post_ids as $post_id ){
$post_attechment_id = get_post_meta( $post_id, '_thumbnail_id', true );
$attechment_ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_value = $post_attechment_id" );
if( isset( $item['post_media'] ) && $item['post_media'] === 'yes' ){
if( count( $attechment_ids ) <= 1 ){
wp_delete_attachment( $post_attechment_id, $force_delete );
}
}
if( $force_delete === false ){
wp_trash_post( $post_id );
}else{
Expand Down
Loading

0 comments on commit f27f24c

Please sign in to comment.