Skip Navigation

[Resolved] Delete all media except Featured Image when cpt is set to taxonomy "Sold"

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Henk Schievink 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1621079

Tell us what you are trying to do?
I'm working on a real estate site with a CPT called "Woningen" and I want to delete all attached Images/media except the Featured Image when a "woning" (House) is set to the custom taxonomy "Verkocht". (Sold)

Is there any documentation that you are following?
Found something that comes close to what i want but does not filter on taxonomy, only on post Status "Publish"
It removes the images and media except the featured on saving but does not remove the Toolset gallery (empty) thumbs in the backend and still show the filename.

Is there a similar example that we can see?

add_action('save_post', function($post_id, $post, $update){

if ( $post->post_type === 'Woning' && $post->post_status === 'publish' ) {

$attachments = get_attached_media( 'image', $post->ID );
$featured_id = get_post_thumbnail_id();

//unset the featured image by ID if it exists
unset($attachments[$featured_id]);

foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, false );
}

}

}, 10, 3);

What is the link to your site?
hidden link

#1622255

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Here is the official Doc which states how you can use the save_post method with Toolset:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

For example:

function func_delete_related_custom_images( $post_id, $post ){
    if ( 'woningen' == $post->post_type ) {

                     if( has_term('term-slug', 'taxonomy-slug',$post_id) ){
                                   // do something
                       }

                 
    }
}
add_action( 'save_post', 'func_delete_related_custom_images', 30, 2 );

Where:
- You can use the has_term() function to check if specific term assigned to your post. Repace the term and taxonomy slug.
=> https://developer.wordpress.org/reference/functions/has_term/

I am not sure how Toolset Gallery is setup, is it setup as repeating image field, if yes, you need to remove the repeating field using the function: delete_post_meta()
=> https://developer.wordpress.org/reference/functions/delete_post_meta/

For example:

delete_post_meta($post_id,'wpcf-your-gallery-field-slug');
#1630923

Thank you very much!
It's working now, but the post type should be woning (single) instead of woningen(plural).
so:

function func_delete_related_custom_images( $post_id, $post, $Update ){
if ( 'woning' == $post->post_type ) {

if( has_term('verkocht', 'status', $post_id) ){

// Delete meta

delete_post_meta($post_id,'wpcf-your-gallery-field-slug')

$attachments = get_attached_media( 'image', $post->ID );
$featured_id = get_post_thumbnail_id();

//unset the featured image by ID if it exists
unset($attachments[$featured_id]);

foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, false );
}
}

}
}
add_action( 'save_post', 'func_delete_related_custom_images', 30, 3 );

So it's working on a post by post basis, which is tedious.

That's why i tried to use the bulk edit function to save/update multiple posts with the status "sold" all at once.
This removes all attachments and meta but somehow also removes the featured images of these post. How is that possible?
What am i missing?

#1631479

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can see that you added the code to that is used to unset the featured image:

$attachments = get_attached_media( 'image', $post->ID );
$featured_id = get_post_thumbnail_id();

//unset the featured image by ID if it exists
unset($attachments[$featured_id]);

I already guide you how you can use the save_post hook with Toolset, now you need to add the custom code as per your requirement. If you are not sure how it works, you may also consider taking help fro Pro Developers.

#1631655

My issue is resolved for now. Thank you!

The code works when saving or updating a post but only partially when using the "Bulk edit" option.
I will figure out why there is a difference in how wordpress handles the save_post hook differently when using "bulk edit". I think this is out of scope for Toolset.