Auto-fill post_title for WordPress pictures
WordPress is so good for SEO in part thanks to the large number of data fields available for each post: post title, excerpt, name (slug) and, finally, content. This allows for deep customization when presenting your post to the WWW.
While it’s fine to fill all those fields for a regular article it may be an overkill for a picture for that same article (which is just a sub-post in terms of WordPress built-in media library). (And even for a normal article – how many of us really do enter post excerpts? Yes, that’s what I’m talking about.)
Filling out all the fields is becoming ever more cumbersome for a larger “gallery”. It takes some really notable creativity to compose separate title and caption for each of 50+ pictures one usually uploads after a photo session.
No wonder, picture title (which is post_title actually) gets left untouched quite often – it is already pre-filled with image filename by default, while picture caption (post_excerpt) sports stronger incentive to be filled – because that’s the text appearing immediately below the picture in the gallery.
All that is not so bad… until you go about implementing exotic things like random image widget (or anything else using functions of the wp_get_attachment_link() family) and realize that pictures generated have their “title” attribute set to the cryptic image numbers – just as they came out of the digital camera. It’s OK in the context of a large photo gallery where the visitor is at lease aware of where they are at the moment. Problem is when a vague image pops up in a sidebar and has even more vague tool-tip (the title) akin “DSC_0123”. Not a pretty user experience.
The quick fix here seems to be in duplicating image caption to the title field for those images which have the caption set AND which don’t have their title customized already.
To find out potential candidates for this fix-up use the following SELECT statement:
SELECT post_name, post_title, post_excerpt
FROM wp_posts
WHERE post_type='attachment'
AND LENGTH(post_excerpt)>2
AND post_name=LOWER(post_title);
Then, when you see it would be of benefit to auto-replace all the boring numbers in the post_title column, use the following UPDATE to make the change:
UPDATE wp_posts
SET post_title=post_excerpt
WHERE post_type='attachment'
AND LENGTH(post_excerpt)>2
AND post_name=LOWER(post_title);
(notice the same WHERE clause)
Now surely your visitor will feel more like clicking through the image.
Hopefully this will add a bit of SEO too. Just keep in mind that those captions may easily get duplicates – if you’re not Shakespeare of the blogosphere. And, as post_title is used in constructing page titles you better watch not to get duplicate ones. So if you suspect you might have got some dupes captions, use this select to find out for sure – which image titles have duplicates – and go change them manually.
SELECT post_name, post_title, post_excerpt, COUNT(post_title) cnt
FROM wp_posts
WHERE post_type='attachment'
GROUP BY post_title
HAVING COUNT(post_title) > 1;