Setting Featured Image via code

In WordPress the term “Featured Image” is the name of the image related to a post. It is most common to use the featured image for blogposts so there is visual material on the website and the archive page is a lot nicer to look at.

I have been gradually updating a WordPress blog over the past few weeks, and one of the new features requested was adding more imagery to the site.

The previous layout was plain and boring and adding photos to the blogposts was a quick way to update the style.

A slight problem, however. The WordPress installation have thousands of blogposts and setting a featured image for each of them would have taken days, perhaps even weeks!

I was lucky that the site was very niche specific and a lot of general imagery could be found that could be used throughout the site, and I will now share with you an easy way to assign featured images to every single post…

The problem

WordPress have had the Featured Image feature built in for a while, but there is no code-function (that I could find) that I could use to ASSIGN a specific image(URL) to a blogpost.

After digging through WordPress’s code, searching high and low on Google, I realized I would have to make my own solution to fix this.

While searching through the code I saw that the way the Featured Image information is stored is to via a custom field with the name ‘_thumbnail_id’ and then the value of the attachment id of the picture. The referring meta value is then linked to another meta key with serialized data which is generated while uploading the image and then… You get the picture.

Now, the optimal coding solution would be to create a script that would automatically upload an image using WordPress’s built-in functions and have the image processed, grab the attachment ID, and then add that to the blogpost.

That would be a lot of coding, for a once-off project. Way too complicated. I chose the semi-automated way.

The semi-lazy solution

I picked 30-40 random blogposts and went to edit them each.

For each post I chose a picture from the stock library and attached it as a featured image.

Now, that sorted out those blogposts, but what about the rest? Here’s how I added a random featured image to the other thousands of blogposts.

I added the following code to my functions.php

This solution is not for everyone and if you do not know what you’re doing, do not try this code. There is no undo!

Now, the code in itself is quite simple. I add a filter to ‘the_content’, which means the code will run every time a post is visited.

I added an if (is_single()) to detect if it is a single blogpost or not. (I’m not interested in triggering this code on pages, tags and category page views…)

The code then checks if there already is a featured image added to the post. If there isn’t I make a MySQL query from the postmeta table to find a RANDOM featured image, and grab the meta_value of that. Once that is grabbed, I attach it as a meta value to the current post, and voila… The post now has a featured image, which is then used in the theme throughout the site.

In a week or so, I will revisit the site and remove the code. The site is well visited, so all posts will have an image attached by then, and I will no longer need the code.

You could also run a crawler on the website, that would trigger all the page views necessary to set the featured image on all blogposts.

Before and after

The pictures before and after show the much improved first impression with nice imagery.

Before and after auto assigning featured image

But, why not…?

I could have chosen to create a MySQL query, but I am no database master and I was afraid of ruining something on an otherwise stable site.

I could have used a more general piece of code that just took a random image and displayed it as a featured image, but that would mean random images for each visit and an ever-changing html source code.

Using this method will also ensure that each blogpost will have a featured image attached to it, and by using WordPress built-in Featured Image functionality I know that if I ever need to change the theme, the imagery for each blog post will not change.

(Most premium WordPress themes support the built-in functionality).

I also chose this method to add the imagery since I like the idea of the visitors (and search engine crawlers) doing my work 🙂

Do you have a better method, or are you a MySQL master that can create a query to do all this with a single piece of code? Don’t be shy, leave a note and tell me!

Cleverplugins newsletter

Never miss a story or discount - sign up now

Cleverplugins Newsletter

Never miss a story or discounts - sign up now