Write less, do more, indeed

I moved a bunch of Namine’s photo albums off to Picasa (which is now inside Google+, go figure).

Now I had a bunch of photos to remove from WordPress, and there doesn’t seem to be any good way to remove all the pictures associated with a post or page.

Sure, the “Add Media” button allows you to select photos uploaded for a post, and even though you can select multiple photos, you can’t batch delete.

The media page wasn’t any better; it allowed me to sort pictures by the post they were uploaded for, but not group select. I still had to click, click, click my way through.

Manually click checkboxes three hundred twenty-four times? I don’t think so.

There must be a better way!

I write JavaScript (among other things) for a living, so I could figure this one out. I knew I wanted to check the image checkboxes for a specific post, and I knew the post’s URL as displayed by the media page. So I whipped up this bad boy (the “post=xxxx” value being the thing you’d change to use for your own purposes) and ran it in the console:

jQuery('a[href="http://www.eichefam.net/wp-admin/post.php?post=6247&action=edit"]').closest('tr').find('input[type="checkbox"]').attr('checked', 'true')

What ends up happening is really very simple. First, we’re getting all the listed links to a specific post. Next, the jQuery function closest() looks up the DOM tree for a first match. In this case, the parent table row. From there, we just need to find the image’s checkbox. There’s only one in each table row, so we don’t need to get any more precise than a simple find(). Lastly, we modify the “checked” property to “true.” And wah lah, we’ve just selected an obscene amount of checkboxes.

After that, all I had to do was hit “delete.” Done and done.

6 responses

  1. I love Matt’s reply!!

Leave a Reply to Michele Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related