How to get an attachment id from a filename

I worked this out eventually by tracing through the code for get_attached_file() to see where it was getting the filename from, and reverse engineered the following: function get_attachment_id_by_filename($filename) { global $wpdb; $sql = $wpdb->prepare(“SELECT * FROM $wpdb->posts WHERE post_type=”attachment” and guid like %s order by post_date desc”, “%$filename”); $attachments = $wpdb->get_results($sql, OBJECT); return $attachments[0]->ID ?? … Read more

Set term on an attachment using wp_set_object_terms and want to display the full term text but it’s showing a slug instead

When a visitor fills out the email address field with “[email protected]”, what is displayed on the Attachment details screen (as in siteurl.com/wp-admin/upload.php?item=xx ) for that email field is “nameemail-com”. Yes, and that is the default behavior, i.e. WordPress displays the term slug instead of name (or the actual email address in your case). But there’s … Read more

How to get comment images stored as serialized comment meta

What you get is fine, it’s the image id, now you just need to loop them and output the structure that you want, for example. if (!empty($images = get_comment_meta( $comment->comment_ID , ‘reviews-images’, true ))) { foreach ($images as $image_id) { $image_src = wp_get_attachment_image_src($image_id, ‘full’); // Now you have the image src, $image_src, you can do … Read more

How to query details of images in gallery that is attached to a post

Query for image attachments would be something like this (taken from Get The Image plugin/extension) with get_children(): $attachments = get_children( array( ‘post_parent’ => $args[‘post_id’], ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => ‘ASC’, ‘orderby’ => ‘menu_order ID’ ) ); Then you can loop through array and retrieve URLs with wp_get_attachment_image_src() which will … Read more

attachment page template? only show attachments for current post?

The attachment.php is a specific template in the WordPress template hierarchy. WordPress uses it for a specific purpose, and the default Loop just won’t do what you’re claiming it’s doing. (The behavior you’re describing would be analogous to the single.php template file looping through all Posts.) I’m guessing your attachment.php template file has a custom … Read more

How can I display a link to a post’s (Word doc or pdf) attachment?

Try this: <?php if ( $attachments = get_children( array( ‘post_type’ => ‘attachment’, ‘post_mime_type’ => array(‘application/doc’,’application/pdf’), ‘numberposts’ => 1, ‘post_status’ => null, ‘post_parent’ => $post->ID ))); foreach ($attachments as $attachment) { echo ‘<a href=”‘ . wp_get_attachment_url( $attachment->ID ) . ‘”>Download Spec Sheet</a>’; echo ‘</div>’; } ?>

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