标记所有Wordpress帖子

时间:2014-01-03 10:57:30

标签: wordpress tags

我一直在寻找几个小时,无论是插件还是一些安全的SQL来做这件事,但似乎没有什么是我需要的......

我有一个大约32,000个帖子的Wordpress网站,我们使用各种标签来帮助管理后端(即在前端看不到它们或用于搜索引擎优化,之后有人评论过多使用相同的标签)。这些帖子中大约有30,000个包含“新”标签,但我们现在需要标记其余的〜2,000个以匹配。

WP管理面板并不适合完成任务 - 需要花费一些时间才能完成并将标签应用于2,000个帖子。各种插件似乎都存在,但它们并没有真正按照它们在描述中所声称的那样做。我能找到的唯一有用的SQL假设标签是新的,我想将它应用于单个类别中的所有帖子。我想我可以删除现有的标签(再次,用Wordpress的管理面板不是一个有趣的任务 - 它通常在大约30个帖子后崩溃,这意味着有人必须一遍又一遍地点击按钮)然后运行SQL来应用新的。

有人可以指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

您可以对所有帖子进行循环,并使用wp_set_object_terms()添加所需的标记:

http://codex.wordpress.org/Function_Reference/wp_set_object_terms

答案 1 :(得分:0)

我今天也遇到了这个问题。我使用以下SQL标记了所有帖子。 (将<prefix>替换为您的数据库前缀,将"GlobalTag"替换为您想要的标记名称。)

# create a term which will be your new tag
INSERT INTO <prefix>_terms (name, slug) VALUES ("GlobalTag", "global-tag")

# this is what defines the term as a Tag vs Category vs Video Category, etc
# only do this if you are creating the category through SQL, not the GUI
INSERT INTO <prefix>_term_taxonomy (term_id, taxonomy, count)
SELECT
  (SELECT term_id from <prefix>_terms AS term WHERE term.name="GlobalTag") as "term_id",
  "tag" as "taxonomy",
  (SELECT COUNT(*) FROM <prefix>_posts AS posts WHERE (posts.post_status IN ("publish", "draft")) AND LENGTH(post_content) > 25) as "count"

# now tag every post with GlobalTag
INSERT INTO <prefix>_term_relationships
SELECT
  id as "object_id",
  (
    SELECT term_taxonomy_id
    FROM <prefix>_term_taxonomy AS term_tax
    INNER JOIN <prefix>_terms ON <prefix>_terms.term_id = term_tax.term_id
    WHERE <prefix>_terms.name="GlobalTag"
  ) as "term_taxonomy_id",
  0 as "term_order"
FROM <prefix>_posts AS posts
WHERE (posts.post_status IN ("publish", "draft")) AND LENGTH(post_content) > 25

注意:您可能需要在此处调整帖子查询。此标记所有已发布和/或草稿且内容超过15个字的帖子。