移动基于mysql列的wordpress媒体文件(根据日期重新组织)

时间:2016-06-13 17:42:10

标签: php mysql wordpress file migrate

我有一个wordpress网站,用户选择不按日期组织媒体。现在,2000年左右的图像,他们希望它按日期组织。

我已将我的查询放在一起,根据父帖的日期更改我的网址。

然而,现在我必须着手实际将图像移动到他们的新家。

我启动了一个php脚本,试图根据附加的父帖子的日期来移动文件。

$wp_path = getenv('ACT_PATH');
if(is_null($wp_path) || empty($wp_path)) {
        echo('ERROR: Environment variable $ACT_PATH must be defined'. "\n");
        exit(10); 
} 
require_once($wp_path . 'wp-load.php'); 
require_once($wp_path . 'wp-includes/post.php'); 
$query = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => null 
); 
$attachments = get_posts($query);
/**  
* Inserts year and month components to the provided array of guid parts 
* @param array $bits Parts of a file guid to be modified  
* @param int $year Year to insert into file guid  
* @param int $month month to insert into file guid  
*/ function addDateParts(&$bits, $year, $month) {
        $base_index = array_search('uploads', $bits);
        if($base_index !== false) {
            $suffix = $bits[$base_index + 1];
            $bits[$base_index + 1] = $year;
            $bits[$base_index + 2] = $month;
            $bits[$base_index + 3] = $suffix;
        }
  } 
echo('Source,Upload Date,Destination'. "\n"); foreach ($attachments as $a) {
        $time = strtotime($a->post_date_gmt);
        $year = date("Y", $time);
        $month = date("m", $time);
        $guid_bits = explode('/', $a->guid);
        addDateParts($guid_bits, $year, $month);
        $new_guid = implode('/', $guid_bits);
        echo($a->guid . ',' . $a->post_date_gmt . ',' . $new_guid . "\n"); 
}

但我发现自己有点坚持下一步该做什么。任何帮助表示赞赏。

0 个答案:

没有答案