Creating Wordpress Featured Images from post images

Hello!

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

Bill
LVL 1
Bill HendersonWeb MarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineDon't talk to me.Commented:
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.
0
Bill HendersonWeb MarketingAuthor Commented:
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.

Bill
0
Jason C. LevineDon't talk to me.Commented:
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:

http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_10258-How-To-Move-A-WordPress-Site.html

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason C. LevineDon't talk to me.Commented:
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...
0
Jason C. LevineDon't talk to me.Commented:
Aw, crap.  I'm an idiot.

Bill, look at this:

https://wordpress.org/plugins/quick-featured-images/

Assuming you migrate the site completely and correctly, that plugin should do what you ask.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.