0
votes

I have a list of post-slugs retrieved from Google Analytics (1000+) and I want to tag those posts with specific tag in bulk in Wordpress. They're too many posts to do this manually from the dash.

I was trying to get a post by its slug and then for each post to add the specific tag using the wp_set_post_tags function, but it seems the argument 'name' doesn't allow adding slugs in an array.

'name' => array('slug_1', 'slug_2', 'slug_etc');

I can't get it working, and I'm pretty sure this should be a fairly simple task.

1

1 Answers

0
votes

I think I've got it.

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 );

Note: You can use post_name__in only if you have Wordpress 4.4 and up.