1

I have a custom-post-type called "newcomer". I am trying to put together a function which would delete all image attachments (except featured image) of a custom-post-type when the CPT post is updated or published. But this does not seem to be working, what am I doing wrong ?

function delete_extra_images() {
        if ( $post->post_type == 'newcomer' && $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id()
            ));

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

    }
    add_action( 'publish_newcomer', 'delete_extra_images' );

1 Answer 1

1

Try this on for size:

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

    if ( $post->post_type === 'newcomer' && $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);

Note: You can forgoe wrapping the foreach statement in a conditional checking the state of the $attachments variable as get_attached_media() casts its result to an array internally. Of course if you wish to be prudent in the event core internals change then just cast it yourself or add back your conditional check.

Useful reference: http://codex.wordpress.org/Function_Reference/get_attached_media

4
  • works nicely, i should have tried save_post. Is there a way my code would have worked with publish_"custom_post_type", basically I would like to know where my code has gone wrong.
    – gurung
    Commented Jun 1, 2015 at 11:03
  • @gurung Using the publish_{post_type} hook will also work but be aware that this hook will only fire when a post of your desired type transitions to the status of publish from any other status, for example from draft to publish. See codex.wordpress.org/Post_Status_Transitions for reference. Most likely in your scenario, save_post is the more suitable hook for your needs.
    – Adam
    Commented Jun 2, 2015 at 1:23
  • for what purpose are these arguments here $post_id, $post, $update ??
    – gurung
    Commented Jun 2, 2015 at 5:24
  • 1
    @gurung These arguments are passed to your callback function in wp-includes/post.php line 3541, you will note: do_action( 'save_post', $post_ID, $post, $update );. Thus they are available for use within your callback, respectively $post_ID is the Post ID of the current object being saved, $post is the entire current WP_Post object and $update denotes (to quote core) "Whether this is an existing post being updated or not."
    – Adam
    Commented Jun 2, 2015 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.