Creating Wordpress Featured Images from post images

Posted on 2014-12-17
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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now