Unable to update guid of image posts using $wpdb->update
-
Hello, everyone. I’m a WP developer and I’m stuck with a problem I hope you might be able to help me with.
I have a new client who has been using WP for his travel blog for around 2 years or so, with the Media setting “Organize my uploads into month- and year-based folders” unchecked, and has asked me to fix this retroactively for all images.
For now, I have successfully:
1. Copied all the images into year\month folders usingfilemtime()
anddate()
to get the year and the month. No pb there.
2. Updated all references to said images inside the post_content column, in the wp_posts table.But I’m failing at updating each image post’s guid using
$wpdb->update()
. Here’s the script I’m running in order to accomplish this:function sep($str) { return str_replace(array("/", "\\"), SEP, $str); } function update_db_guids() { global $wpdb; //$db = @file_get_contents(OLDDB); $query_images_args = array( 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1, ); $query_images = new WP_Query( $query_images_args ); foreach ( $query_images->posts as $image) { $ID = $image->ID; $url = wp_get_attachment_url( $ID ); $url_r = explode("uploads/", $url); if( strpos($url_r[1], "/") === false ) { $year = date('Y', filemtime(PATH.$url_r[1])); $month = date('m', filemtime(PATH.$url_r[1])); $new_url = $url_r[0].$year."/".$month."/".$url_r[1]; $wpdb->update ( 'wp_posts', // table array( 'guid' => $new_url ), // data array( 'ID' => $ID ), // where array( '%s' ), // data format array( '%d' ) // where format ); } } } set_time_limit(360); define("SEP", DIRECTORY_SEPARATOR); define( "PATH", sep(WP_CONTENT_DIR."/uploads/") ); update_db_guids();
However, after running this script and checking the targeted image posts, their guids remain unchanged.
Can anyone shed some light on what I may be doing wrong? Thanks in advanced!
- The topic ‘Unable to update guid of image posts using $wpdb->update’ is closed to new replies.