Get wp_get_attachment_url outside of loop

if the result you’re looking for is a printout of the URL, like in your example, then this should work: $page_id = get_queried_object_id(); if ( has_post_thumbnail( $page_id ) ) : $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $page_id ), ‘optional-size’ ); $image = $image_array[0]; else : $image = get_template_directory_uri() . ‘/images/default-background.jpg’; endif; echo $image;

Edit image / Delete image link

This isn’t 100% complete, but should be a good start for you. <a href=”https://wordpress.stackexchange.com/questions/31371/<?php echo wp_nonce_url(“/wp-admin/post.php?action=delete&amp;post=$post->ID”, ‘delete-attachment_’ . $post->ID ) ?>”><?php _e( ‘Delete Permanently’ ) ?></a> A couple notes on what you’ll have left to do: This will do no confirmation, so you’ll probably want to use JS to verify that it wasn’t clicked by … Read more

List most recent image uploads, but only for specific custom post type

Did you try adding a filter to get_posts. This isn’t tested, just a thought after a bit of searching : function my_filter( $query ) { if( is_home() ) //Example, if u want to display recent images on home { $query->set( ‘post_type’, array( ‘attachment’, ‘my_cpt’ ) ); } } add_filter( ‘pre_get_posts’, ‘my_filter’ ); EDIT : After … Read more

Remove duplicate attachments

function get_attachment_files(){ $args = array( ‘post_type’ => ‘attachment’, ‘numberposts’ => -1, ‘post_status’ => null, ‘post_parent’ => 0 ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $post) { setup_postdata($post); wp_delete_post( $post->ID ); } } } add_action(‘admin_init’,’get_attachment_files’); adapted from: http://wpsnipp.com/index.php/functions-php/list-all-unattached-files-in-media-library/ I’d be careful of this though, because I am not sure it won’t delete the … Read more

complex restriction of items in media library

Good code. I think you could simplify by making direct SQL queries instead: — Featured Images SELECT `meta_value` FROM `wp_postmeta` WHERE `meta_key` = ‘_thumbnail_id’; –> $thumbnail_ids — Header Images SELECT `post_id` FROM `wp_postmeta` WHERE `meta_key` = ‘_wp_attachment_context’ and `meta_value` = ‘custom-header’; –> $header_ids Also, with your method, I think that if you call WP_Query with … Read more

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