Go Premium for a chance to win a PS4. Enter to Win


Creating Wordpress Featured Images from post images

Posted on 2014-12-17
Medium Priority
Last Modified: 2014-12-17

I have a Wordpress site I inherited where all blog posts currently do not use the Featured Image function in Wordpress (latest) but instead the post starts with an image.

I'm hoping to do a few things, but I don't understand the Featured Image architecture well enough to understand what is going on here.

So I have the exported XML which I assume to be the best place to try and make a modification.

And i see each <item> being a post.

And each item includes the key for content:

<content:encoded><![CDATA[<img class="aligncenter size-full wp-image-7380" alt="widish-gifts-to-melt-away-stress" src="/wp-content/uploads/2014/12/widish-gifts-to-melt-away-stress.jpg" width="634" height="467" />

Is there a way to:

1. Strip out this image at the start of posts
2. Insert it as an associated Featured Image for the same item (post)
3. Save the revised XML

When I look at an item in the XML that does contain a featured image, the only difference I see is a thumbnail key, but if it is pulling an asset ID from that field, we may be stuck.

Any suggestions? Does anyone understand how this could work?

Thank you

Question by:billium99
  • 4
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40504930
So I have the exported XML which I assume to be the best place to try and make a modification.

And that would be your first mistake.  It isn't the best place, isn't even a place to make the modifications.  You want to work directly with the database table, specifically wp_posts and wp_postmeta.  The XML file can be altered but you would then have to delete all content on the site and reimport the XML.  This will cause tons of problems.  Always work in the database if you can.

1. Strip out this image at the start of posts

First, you will want to save the image path and the post ID somewhere.

I would do this using a regular expression to match the first <img> tag in a post and remove.  While you can do this with queries, my query-fu is iffy with this so I would work around that by dumping the wp_posts table to a SQL file and then using a good text editor to do the work, then replace wp_posts when done.

2. Insert it as an associated Featured Image for the same item (post)

Much harder to do.  Featured images are saved in wp_postmeta so you would need to create new entries in that table for each post. So the ID and path we saved in step one forms the basis of an Insert query that build each featured image in the database.

3. Save the revised XML

Moot, since the database is being directly altered.

Author Comment

ID: 40505206
I don't think I was clear. I'm migrating the content to a new website. New Wordpress instance and everything.

However, the current site is live and cannot be altered. The database tables cannot be altered. It's fine if I must do this all in the database on the new site, but that was why I was hoping to make the changes during the XML export/import process.

In any case, can you elaborate on "using a regular expression" in a text editor? Is this possible? We have hundreds of posts, so I'm not sure there is a clean way to get there in any sort of automated fashion.

LVL 70

Accepted Solution

Jason C. Levine earned 2000 total points
ID: 40505268
I don't think I was clear. I'm migrating the content to a new website.

Ah-ha.  That changes things a little bit.  I still don't like using the XML file for this, though.  I talk about this in this article:


The best way to move is to dump out the complete WordPress database to a text file and then use something like Sublime, Programmer's Notepad, or Notepad++ to do any search and replaces needed.  All of those programs (Sublime is paid, PN and N++ are free) support regular expression search and replace...the trick will be coming up with the correct regex to do this.

You're suffering from choices made prior to your taking this one.  Embedding a featured image into a post body is trivial.  Going the other way is not.
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40505365
Bill, thanks for the (mostly undeserved) points.  If you want to ask additional followups, I'm happy to answer to the best of my ability...
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40505371
Aw, crap.  I'm an idiot.

Bill, look at this:


Assuming you migrate the site completely and correctly, that plugin should do what you ask.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question