Tag Info

New answers tagged


Well, there is a WP function for that. wp_get_attachment_url It takes attachment id as argument, so you can use it like in following example: echo wp_get_attachment_url( 12 ); This will echo url to attachment which id is 12. You can find more info and examples on Codex page: https://codex.wordpress.org/Function_Reference/wp_get_attachment_url


I tried to write some snippet with the help of WordPress codex. Please check below and update it further as per your needs. I hope this helps: global $post; $args = array( 'numberposts' => 1, 'order' => 'ASC', 'post_mime_type' => 'image', 'post_parent' => $post->ID, 'post_status' => null, 'post_type' => ...


I figured out how to retitle the attachment myself, using a combination of the answers from these two posts: How can I add a default description to uploaded files? Change attachment filename Here's the code, it's working on my current WP installation (v4.3) function wpse_retitle_attachment( $post_ID ) { $file = get_attached_file($post_ID); $path ...


What I do in such cases in that save the id in a hidden input along with the URL. function meta_image_upload() { global $post; $my_image = get_post_meta($post->ID, '_my_image_metabox', true); $my_image_id = get_post_meta($post->ID, '_my_image_id_metabox', true); $out = '<input id="my_image" type="text" name="my_uploaded_image" ...


All the post meta you're saving is being attached to a post of type attachment, so You can consider $post->ID is the attachment ID.


Try with this: $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type ...


This question seems aged but I'd like to weight-in. I assume you are using WordPress outside of normal operation? If this is the case, to interact with the WordPress core you have to load it manually. This will ensure constants are defined and functions work correctly. Before calling wp_upload_dir() and essentially switch_to_blog() load in the WP ...

Top 50 recent answers are included