Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have hundreds of images in my media library, most have which have no titles. In the media library, the "(no title)" message is displayed along these images.

Since I generally have one image per post, I would like to auto-populate the library titles, by grabbing the associated post name. I don't mind if I overwrite some existing titles.

Does anyone know of a hack that will do this?

I found some plugins, like 'Media Rename' and 'Rename Media' and 'SEO friendly images' but they are usually for renaming the actual image files, or for creating HTML title tags directly in the HTML (on the fly).

I want to create titles within the Wordpress media library, so that I don't see the (no title) message. This title data will also help me to search for images in the library -- although, for anyone with the same issue, I did find a plugin called 'Media Search' that enables searching images by associated posts: http://wordpress.org/extend/plugins/media-library-search/

share|improve this question
2  
"Does anyone know of a plugin or script that will do this". Sorry, but recommendations are off-topic on this site and Shopping Questions are off-topic on all SE sites. Please do some research for code and edit your question to bring it on topic. The faq will help you doing so. Thanks. –  kaiser Feb 15 '13 at 11:14
add comment

1 Answer

up vote 2 down vote accepted

If you can do SQL manually then try:

UPDATE wp_posts p INNER JOIN wp_posts q ON p.post_type = 'attachment' AND p.post_mime_type LIKE 'image/%' AND (p.post_title IS NULL OR LENGTH(p.post_title) = 0) AND p.post_parent = q.ID SET p.post_title = q.post_title;

If you need a PHP function then try:

function set_image_without_title_to_post_title() {
  global $wpdb; 

  $sql = sprintf( "UPDATE %s p INNER JOIN %s q "
    . "ON p.post_type = 'attachment' AND p.post_mime_type LIKE 'image/%%' " 
    . "AND (p.post_title IS NULL OR LENGTH(p.post_title) = 0) "
    . "AND p.post_parent = q.ID "
    . "SET p.post_title = q.post_title",
    $wpdb->posts, $wpdb->posts );

  $wpdb->get_results( $sql, ARRAY_A );
}

WARNING: As this will make massive changes to your database I would do a backup first! I did run a small test and it appears to be correct but USE AT YOUR OWN RISK!

share|improve this answer
    
Fantastic, it worked, thanks!!! For any other PHP/MySQL beginners out there, I just backed up my databased using PHPAdmin. Then also in PHPAdmin I clicked on the 'SQL' tab and copied the entire string posted above (top one) into the box, and clicked 'Go'. That was all! –  Michael Heraghty Feb 18 '13 at 10:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.