2

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/

1
  • 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, 2013 at 11:14

1 Answer 1

3

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!

1
  • 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! Feb 18, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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