Skip to content

Commit b74ba38

Browse files
committed
Added script to add tag to all posts
1 parent 8a507a1 commit b74ba38

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

README.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,14 @@
55

66
## Usage
77

8+
See individual scripts for usage
89

910
## Develop
1011

1112
1. `git clone` this repo & `cd` into it as usual
1213
2. Run `yarn` to install top-level dependencies.
1314

1415

15-
## Run
16-
17-
- `yarn dev`
18-
- View: [http://localhost:9999](http://localhost:9999)
19-
20-
21-
## Test
22-
23-
- `yarn lint` run just eslint
24-
- `yarn test` run lint and tests
25-
26-
27-
## Publish
28-
29-
- `yarn ship`
30-
31-
3216
# Copyright & License
3317

3418
Copyright (c) 2019 Ghost Foundation. All rights reserved.

add-tag-to-all-posts.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Add a tag to every post
3+
*
4+
* Note: Assumes you already have the tag created
5+
*
6+
* Usage:
7+
*
8+
* node add-tag-to-all-posts.js https://blah.ghost.io ADMIN_API_KEY slug-of-tag-to-add
9+
*/
10+
11+
if (process.argv.length < 5) {
12+
console.error('Missing an argument');
13+
process.exit(1);
14+
}
15+
16+
const url = process.argv[2];
17+
const key = process.argv[3];
18+
const tag = process.argv[4];
19+
20+
const Promise = require('bluebird');
21+
const GhostAdminAPI = require('@tryghost/admin-api');
22+
const api = new GhostAdminAPI({
23+
url,
24+
key,
25+
version: 'v2'
26+
});
27+
28+
(async function main() {
29+
try {
30+
console.log(`Adding tag ${tag} to ${url}`);
31+
32+
const tagToAdd = await api.tags.read({ slug: tag });
33+
34+
console.log('Found tag', tagToAdd);
35+
36+
// Admin API automatically includes tags and authors
37+
// WARNING: If the site is really big (1000s of posts) maybe do this paginated
38+
const allPosts = await api.posts.browse({ limit: 'all' });
39+
40+
// convert our list of posts, to a list of promises for requests to the api
41+
const result = await Promise.mapSeries(allPosts, async (post) => {
42+
43+
// Add the tag to the post
44+
post.tags.push(tagToAdd);
45+
46+
// Weird.. we shouldn't need to do this:
47+
delete post.uuid;
48+
delete post.comment_id;
49+
50+
console.log('Updating', post.slug);
51+
// Call the API
52+
let result = await api.posts.edit(post);
53+
54+
// Add a delay but return the original result
55+
return Promise.delay(50).return(result);
56+
});
57+
58+
console.log(`Updated ${result.length} posts`);
59+
60+
} catch (err) {
61+
console.error('There was an error', require('util').inspect(err, false, null));
62+
process.exit(1);
63+
}
64+
}());

0 commit comments

Comments
 (0)