仅通过MySQL为WordPress添加带特色图片文章失败,求排查
排查:仅用SQL给WordPress添加带特色图片的文章失败
嘿,我帮你梳理下问题——WordPress的特色图片机制比你想象的要多几步,光加主文章和普通元数据是不够的,咱们一步步来排查和修正:
你大概率遗漏的关键环节
WordPress的特色图片不是直接存在主文章里的,它是通过「附件文章+元数据关联」实现的,你可能踩了这些坑:
- 没在
wp_posts里创建对应图片的附件记录:特色图片本质是wp_posts中一条post_type = 'attachment'的独立记录,你可能只加了主文章,没建这条记录。 - 主文章缺了
_thumbnail_id元数据:主文章的wp_postmeta必须有一条meta_key = '_thumbnail_id'的记录,值是附件的ID,这是关联特色图片的核心。 - 附件记录的关键信息不全:比如附件的
post_parent要指向主文章ID,同时还得在wp_postmeta里加图片路径、尺寸这些必要元数据。
正确的SQL操作流程(以你的主文章ID=62为例)
假设你已经把图片传到了wp-content/uploads/2024/05/test-image.jpg,下面是完整的SQL步骤:
1. 先插入图片的附件文章记录
INSERT INTO wp_posts ( ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count ) VALUES ( 63, -- 这个ID要记好,后面关联要用 1, -- 一般是管理员的用户ID,默认是1 NOW(), NOW(), '', '我的特色图片', '', 'inherit', 'closed', 'closed', '', 'wo-de-te-se-tu-pian', -- 附件的slug,拼音或英文都行 '', '', NOW(), NOW(), '', 62, -- 这里填你的主文章ID=62,关联起来 'http://你的域名/wp-content/uploads/2024/05/test-image.jpg', -- 图片的访问地址 0, 'attachment', 'image/jpeg', 0 -- 图片类型是jpeg,png就改成image/png );
2. 给主文章加特色图片关联
INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (62, '_thumbnail_id', 63); -- 63就是刚才插入的附件ID
3. 给附件加必要的元数据
-- 记录图片在uploads里的相对路径 INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (63, '_wp_attached_file', '2024/05/test-image.jpg'); -- 记录图片的尺寸信息,要根据你实际图片的尺寸改 INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (63, '_wp_attachment_metadata', '{"width":1200,"height":800,"file":"2024/05/test-image.jpg","sizes":{"thumbnail":{"file":"test-image-150x150.jpg","width":150,"height":150,"mime-type":"image/jpeg"},"medium":{"file":"test-image-300x200.jpg","width":300,"height":200,"mime-type":"image/jpeg"},"large":{"file":"test-image-1024x683.jpg","width":1024,"height":683,"mime-type":"image/jpeg"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}}');
最后再检查这几点
- 主文章的
post_status是不是publish?要是草稿或待审,前端肯定看不到。 - 附件的
post_mime_type和图片格式匹配吗?比如webp要写image/webp。 - 你的WordPress表前缀不是
wp_的话,记得把所有表名换成你自己的前缀。
内容的提问来源于stack exchange,提问作者Lora
相关产品推荐
相关产品推荐

