【问题标题】:Tag posts in Wordpress (if you only know the post slugs)在 Wordpress 中标记帖子(如果您只知道帖子 slug)
【发布时间】:2016-10-28 02:41:53
【问题描述】:
我有一个从谷歌分析(1000+)检索到的帖子列表,我想在 Wordpress 中批量标记这些帖子。他们的帖子太多了,无法从破折号手动执行此操作。
我试图get a post by its slug,然后为每个帖子使用wp_set_post_tags 函数添加特定标签,但似乎参数“名称”不允许在数组中添加 slug。
'name' => array('slug_1', 'slug_2', 'slug_etc');
我无法让它工作,我很确定这应该是一个相当简单的任务。
【问题讨论】:
标签:
php
wordpress
tags
slug
【解决方案1】:
我想我明白了。
function add_tags() {
global $post;
$args = array(
'post_name__in' => array('slug_1', 'slug_2', 'slug_3', 'slug_etc'),
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach($posts as $post) {
$id = get_the_ID();
wp_set_post_tags($id, 'Your_Tag_Here', true);
}
}
add_action( 'admin_init', 'add_tags', 0 );
注意:只有在您拥有 Wordpress 4.4 及更高版本时才能使用post_name__in。