get_attached_media() returns empty array if media file already used by another post

I ran into this problem and ended up creating a basic function to extract the “attached” media by URL from the body of the post (in my case a post of type document, but it should work with any kind of post): function get_first_link_url($post_id) { $content = get_post_field(‘post_content’, $post_id); if ($content == null) { return … Read more

Validate alt text for attachments?

The code below will only run once whenever a file is uploaded. We keep an array ( $image_mimes ) of acceptable image-mimetypes We get the current attachment mime type We make sure what is given is indeed an image ( because we don’t need unnecessary postmeta cluttering our table ) We grab the title from … Read more

How can I UN-attach media from a post?

I learned from Brian Fegter that you can Un-attach an image from a post while participating in this question: Can I attach an image to a different post? It isn’t exactly “simple”, but not to difficult. It’s the only solution I’ve been able to find.

media_handle_upload for local files?

You want media_handle_sideload() Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload(). // Array similar to a $_FILES upload array. $file_array = array( ‘name’ => ‘filename.jpg’, ‘tmp_name’ => ‘path/to/filename.jpg’, ); // Post ID to attach upload to, 0 for none. $post_id = 0; $attachment_id = media_handle_sideload( $file_array, $post_id … Read more

Find the post an attachment is attached to

So, if you start with this: $all_images = get_posts( array( ‘post_type’ => ‘attachment’, ‘numberposts’ => -1, ) ); Then $all_images is an array of objects. Step through each one: foreach ( $all_images as $image ) {} Inside that foreach, you can use the normal parameters available to the $post object: $image->ID is the ID of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)