0

I have a picture gallery website I put together with various plugins and dirty file editing hacks. For example I had to make sure jetpack infinite scroll plays nice with theme's masonry and lazy loading of images which are read from a custom field! etc..

I grow tired of spending time on making sure plugins and hacks play to nice with each other, and I really want to be able to update my wordpress and plugins etc. So I created my own theme from scratch. I'm pretty sure my theme is ready to be deployed live. But there is one little problem.

I was using "Nelio External Featured Image" plugin, so all of my images are stored with its own custom field "_nelioefi_url". I want to change it to "external_thumbnail". For 20k posts. Before cloning the database offline and using mysql update commands, I wanted to ask if someone knows an easier way.

  • So, you want to replace every instance of _nelioefi_url with external_thumbnail in the *_postmeta table? – codiiv Jan 26 '17 at 17:46
  • yes that is correct @codiiv – Emre Jan 26 '17 at 17:48
3

I just tested this and it works.

<?php 

function toCall(){
    global $wpdb;
    $togos = $wpdb->get_results("SELECT *   FROM $wpdb->postmeta WHERE meta_key='_nelioefi_url'");
    return $togos;
}

foreach (toCall() as $go) {
    global $wpdb;
    $wpdb->update(
                $wpdb->postmeta,
                array(
                    'meta_key' => 'external_thumbnail', // string
                    // 'column2' => 'value2'    // integer (number)
                ),
                array( 'meta_id' => $go->meta_id )
            );
};  ?>

You can call the foreach loop somewhere in a file, just make sure you have enough memory :)

EDIT You can put everything in one function:

function toCall(){
    global $wpdb;
    $togos = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='_nelioefi_url'");


    foreach ($togos as $go) {
        global $wpdb;
        $wpdb->update(
                    $wpdb->postmeta,
                    array(
                        'meta_key' => 'external_thumbnail', // string
                        // 'column2' => 'value2'    // integer (number)
                    ),
                    array( 'meta_id' => $go->meta_id )
                );
    };

}  ?>

Then you can call the toCall() method anywhere you want in a template, or otherwise.

NB: The changes are irreversible. So, make sure you have backups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.