0

So I have a situation with this WordPress site I'm doing where I have to swap the positions of my featured images. I have two per post thanks to the Multiple Post Thumbnails plugin, but now whatever is the first Featured Image now needs to be the second and whatever is the second needs to be the first.

So there's 300 posts already so swapping out them manually is going to be time consuming. I'm wondering if there's a better way to do this using some kind of statement in MySQL to swap these two out. Anyone know of a way to do this?

3
  • Adam, if you find my answer useful please up-vote it and mark as selected. Thank you. Commented Jul 2, 2015 at 1:47
  • The title says "swap one table for another" but the question implies swapping values among rows. Can you clarify? Commented Jul 2, 2015 at 11:51
  • Basically, you're right, Michael. But it could have gone either way.
    – Adam Bell
    Commented Jul 2, 2015 at 17:09

1 Answer 1

0

Create a php script ( fpw-swap-thumbnails.php ) with the code below and put it in root of your site:

<?php
// load WordPress environment
require( 'wp-load.php' );

$args = array(
    'posts_per_page' => -1,
    'post_type' => array( 'post', 'page' ),
    'post_status' => 'publish'
);

// get all published posts of type specified in $args
$posts = get_posts( $args );

foreach ( $posts as $post ) {
    // get thumbnails ids
    $thmbsArray = array(
        get_post_meta( $post->ID, '_thumbnail_id', true ),
        get_post_meta( $post->ID, 'post_secondary-image_thumbnail_id', true )
    ); 
    // if both thumbnails exist then swap their values
    if ( ( '' != $thmbsArray[ 0 ] ) && ( '' != $thmbsArray[ 1 ] ) ) {
        update_post_meta( $post->ID, '_thumbnail_id', $thmbsArray[ 1 ] );
        update_post_meta( $post->ID, 'post_secondary-image_thumbnail_id', $thmbsArray[ 0 ] );
    }
}
?>

NOTE: in post_secondary-image_thumbnail_id replace secondary-image with your registered secondary image id. In $args add any extra post types into post_type array.

Make a backup of your database ( just to be safe ). Execute the script. Check results. Remove the script.

10
  • This will change all the references everywhere....including custom post types?
    – Adam Bell
    Commented Jul 1, 2015 at 17:21
  • Also, I noticed nothing in the script about calling a specific database. Usually they do, or does it just take the info from wp-config.php instead?
    – Adam Bell
    Commented Jul 1, 2015 at 17:46
  • You didn't mention custom posts in your question. I'll edit the answer to include posts of any type. The table names are not explicitly stated but it uses two tables - posts and postmeta in your website's database. Commented Jul 1, 2015 at 18:14
  • Seems to be throwing an error at line 7 which would be 'post_type => array( 'post', 'page' ),
    – Adam Bell
    Commented Jul 1, 2015 at 18:39
  • Never mind....it's was missing a closing ' in post_type.
    – Adam Bell
    Commented Jul 1, 2015 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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