使用 SQL 更新 WordPress 帖子的元值

Update Meta Value for WordPress posts with SQL

我正在尝试为 WordPress 数据库中符合特定条件的所有 post 更新元值。

我创建了一个名为 update_with_false 的 table,其中每一行 "value" 与 wp_posts 中 post 的 post_name 相匹配table。

我需要做的是将 meta_key "included_in_related_articles" 的 meta_value 更改为 wp_posts 中每个 post 的 "true" table 与 update_with_false table 中的行具有相同的 post 名称。

这是我现在的 SQL,但出现语法错误:

UPDATE SET wp_postmeta (post_id, meta_key, meta_value)
select ID, 'included_in_related_articles', 'true' from wp_posts
WHERE update_with_false.value = wp_posts.post_name

希望这是有道理的。我附上了我需要更新的元字段和我创建的额外 table

的一些屏幕截图

谢谢!

如果我理解正确,你想更新 wp_postmeta.meta_value 并将其设置为 true 如果 wp_posts.post_nameupdate_with_false.value.

中找到

您可以先将 wp_posts.post_name 列加入 wp_postmeta table然后通过进一步加入 update_with_false table.

来限制加入的帖子

我在脑海中写下了这个(在这个的帮助下 ER diagram):

update wp_postmeta pm
  inner join wp_posts p on p.id = pm.post_id
    inner join update_with_false x on x.value = p.post_name
set pm.meta_value = 'true'
where pm.meta_key = 'included_in_related_articles'

可以使用子查询来完成,如下所示:

UPDATE `wp_postmeta` SET `meta_value`= 'true' WHERE meta_key = 'included_in_related_articles' AND `post_id` IN (SELECT ID FROM wp_posts AS wps LEFT JOIN update_with_false AS uwf on (wps.post_name = uwf.value) )

其中 SELECT ID FROM wp_posts AS wps LEFT JOIN update_with_false AS uwf on (wps.post_name = uwf.value) 是子查询,将从 wp_posts table.

中给出逗号分隔的 ID