N
N
Ninja Mate2017-04-19 22:25:00
WordPress
Ninja Mate, 2017-04-19 22:25:00

How to remove all product images by product id?

There is a store on wp and you need to remove all pictures for some products.

$post_thumbnail_id = get_post_thumbnail_id( (int)27923 );
//хочу получить аррей, а возвращает только один id, их там больше в галереи товара

        //foreach($post_thumbnail_id as $id){
            $res = wp_delete_attachment( $post_thumbnail_id, true );
// удаляет только одну, а нужно для всех
        //}

How to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pushkarev, 2017-04-19
@AXP-dev

Let's start with the fact that you don't need to do this - ( (int)27923 )
And here's a function for you that will return a list of all the images in the post.

function getAttachmentsPost($post_id = 0) {
    $args = array(
        'post_parent'       => $post_id,
        'post_type'         => 'attachment',
        'post_mime_type'    => 'image',
        'posts_per_page'    => -1,
    );

    if ( $attachments = get_children( $args ) ) {
        return $attachments;
    }

    return [];
}

I think you can manage on your own

P
prokids, 2017-04-19
@prokids

also interested in this question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question