0

In my WordPress blog there are more than 500 posts.

Each post starts with small image.

I want to remove those small images from inside posts and display them as Custom Fields.

It will take me a long time to go through all posts and make the required change.

Is it possible to run a SQL query that will be able to:

  1. Catch the small images.
  2. Remove the small images from inside the post.
  3. Associate small image to a post as a custom field.
Charles
  • 50,943
  • 13
  • 104
  • 142

1 Answers1

0

I don't think sql could do that. Instead, I'd write a small php script that does something like this (in pseudo code, you'll need to finish this) using a parser I just Googled (http://simplehtmldom.sourceforge.net/):

$post_collection = query('get all posts');

for( $i = 0; $i < $post_collection.length; $i++ ) {
  $html =  str_get_html( $post_collection[$i] );
  $that_first_image = $html->find('img', 1);
  query('save $that_first_image to custom field');
  $html->find('img', 1) = '';
  query('save $html over the old post');
}
Jasper Kennis
  • 3,225
  • 6
  • 41
  • 74