Auto Set Featured Images

Most current WordPress themes support featured images. But, let’s say you’re updating a site that uses a theme built before WordPress 3.0. And let’s say that theme uses a custom meta field to define the featured images. And let’s say there’s over 900 posts on the site. There’s no way you’d want to set all those featured images manually.

I found myself in that situation today- so I wrote a little plugin:

1) It checks if the custom meta field (‘FeaturedImage’) is set, and if so, fetches the attachment id based on the url string. That attachment gets set as the featured image.

2) If the custom meta field is not set, it will use the first attachment image it finds.

The majority of the code was lifted from two places. Gavick posted the code about setting featured images. Philip posted the code about fetching attachment ids from URL strings.

I can’t vouch at all for the quality of this plugin- so I’m just putting it on GitHub in case it can save another developer some time. Or maybe even make it a little better. Pull requests welcome.

About Devin

I am a developer based in Austin, Texas. I run a little theme shop called DevPress and help manage a WooCommerce shop with Universal Yums. Find me on twitter @devinsays.

2 Responses

  1. Criss

    Sorry, as I’m no developer, may I ask you to write a little function (on the false-line of this) that pulls a custom field value and makes it the featured image of the post? I’d appreciate. Criss (Italy)

Leave a Reply