Skip to content

Instantly share code, notes, and snippets.

@spencermorin
Created July 5, 2013 19:29
Show Gist options
  • Save spencermorin/5936688 to your computer and use it in GitHub Desktop.
Save spencermorin/5936688 to your computer and use it in GitHub Desktop.
Attach thumbnails to the correct posts if this process fails during import. This is for SQL Azure powered Project Nami. However, the logic is the same for a standard install of WordPress.
<?php
$server_name = 'example.database.windows.net';
$database = 'database-name';
$username = 'user@example';
$password = 'thepassword';
$dbh = sqlsrv_connect( $server_name, array( 'Database' => $database, 'UID' => $username, 'PWD' => $password ) );
$post_thumbnail_ids = sqlsrv_query( $dbh, "SELECT post_id, meta_value as thumbnail_id FROM wp_2_postmeta where meta_key = '_thumbnail_id'" );
$errors = sqlsrv_errors();
if( ! empty( $errors ) )
die( var_dump( $errors) );
$thumbnail_ids = array();
while( $row = sqlsrv_fetch_array( $post_thumbnail_ids, SQLSRV_FETCH_ASSOC ) ) {
$thumbnail_ids[] = $row;
}
$count = 1;
foreach( $thumbnail_ids as $data ) {
$post_parent = $data[ 'post_id' ];
$post_id = $data[ 'thumbnail_id' ];
sqlsrv_query( $dbh, "UPDATE wp_2_posts set post_parent = $post_parent where ID = $post_id" );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment