• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

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
0
billium99
Asked:
billium99
  • 4
1 Solution
 
Jason C. LevineNo oneCommented:
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
 
billium99Author 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. LevineNo oneCommented:
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
 
Jason C. LevineNo oneCommented:
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. LevineNo oneCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now