我正在使用WP-JSON API将我的所有帖子从我的live站点导入到我的开发站点。对于帖子、类别和作者来说,它工作得很好,但当我上传图片时,API会将文件视为当天已上传(例如,在2020年6月,所有文件都会转到wp-content/uploads/2020/06
), 这会导致图像链接中断。如何上传文件,使其与源文件在同一日期上传?
以下是我到目前为止的情况(在哪里$MEDIA
是生产服务器对的响应/wp-json/wp/v2/media
要列出所有媒体文件,请执行以下操作:
# Loop through the JSON response (encoding as base64, so each object is on a seperate line)
for media in $(printf %s "$MEDIA" | jq -r ".[] | @base64")
do
media_body=$(printf %s "$media" | base64 -D)
# Get URL and destination path
source_url=$(printf %s "$media_body" | jq .source_url | tr -d \'"\')
destination_path=$(printf %s "$media_body" | jq .media_details.file | tr -d \'"\')
filename=$(basename "$destination_path")
# Download media file to tmp directory
curl -s "$source_url" > "/tmp/$filename"
# Upload the media file to my dev server (store the response as a variable so we can get the ID)
response=$(curl --request POST "http://localhost/wp-json/wp/v2/media/" \\
-u admin:admin \\
-s \\
--header "cache-control: no-cache" \\
--header "content-disposition: attachment; filename=$filename" \\
--data-binary "@/tmp/$filename" \\
--location)
id=$(printf %s "$response" | jq -r ". | .id")
body=$(printf %s "$media_body" | jq -r ". | {date: .date, date_gmt: .date_gmt, slug: .slug, status: \\"publish\\", title: .title.rendered}")
# This outputs body eg {"date": "2020-05-27T12:12:53", "date_gmt": "2020-05-27T12:12:53", "slug": "cropped-shortcut-icon-png", "status": "publish", "title": "cropped-shortcut-icon.png" }
# Update the image\'s metadata
curl --location --request POST "http://localhost/wp-json/wp/v2/media/$id" \\
-u admin:admin \\
-s -o /dev/null \\
--header \'Content-Type: application/json\' \\
--data-raw "$body"
done