# Replace Post Content using SQL Query

**URL:** <https://forum.ghost.org/t/replace-post-content-using-sql-query/22549>\
**Category:** Developer help\
**Created:** [May 8, 2021, 5:34am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549 "2021-05-08T05:34:53Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![thebear.dev](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/thebear.dev/32/13956_2.png) [@thebear.dev](https://forum.ghost.org/u/thebear.dev)\
**Post date:** [May 13, 2021, 9:02am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/21 "2021-05-13T09:02:15Z")

</div>

Perfect! Thank you for the update 🙂

**EDIT: Output now works fine when tested on a new localhost and on my production. It went through well over 800 posts in just a minute:**

The localhost test output:

```auto
bear@bear:~/Documents/GD/api-demos$ node all-posts-find-and-replace.js "http://localhost:2370" 609cea6b583abaf26b967ca6:a141e90087106731d282234606d46d047fff6c84cce17ea1e8bd815729d7a4a8
Updating welcome
Updating design
Updating write
Updating portal
Updating sell
Updating grow
Updating integrations
Updated 7 posts
bear@bear:~/Documents/GD/api-demos$ 

```

---

<div class="post-metadata">

**Author:** ![thebear.dev](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/thebear.dev/32/13956_2.png) [@thebear.dev](https://forum.ghost.org/u/thebear.dev)\
**Post date:** [May 13, 2021, 10:44am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/22 "2021-05-13T10:44:59Z")

</div>

Hi Naz,

I wonder if you could please point me in the right direction in terms of what script from the `api-demos` I should be using for changing the tags on multiple posts at once.

**Example** :  
The site has a total of 100 posts. 40 of them are set to paying members only whereof the remaining 60 are set to public. If I were to remove the paying member tag from those 40 posts, which api-demos file / script should I be using?

**Could I use the `add-tag-to-all-posts.js` file and if so, how would I modify it?**

```auto
/**
 * Add a tag to every post
 *
 * Note: Assumes you already have the tag created
 *
 * Usage:
 *
 * node add-tag-to-all-posts.js https://blah.ghost.io ADMIN_API_KEY slug-of-tag-to-add
 */

if (process.argv.length < 5) {
    console.error('Missing an argument');
    process.exit(1);
}

const url = process.argv[2];
const key = process.argv[3];
const tag = process.argv[4];

const Promise = require('bluebird');
const GhostAdminAPI = require('@tryghost/admin-api');
const api = new GhostAdminAPI({
    url,
    key,
    version: 'v2'
});

(async function main() {
    try {
        console.log(`Adding tag ${tag} to ${url}`);

        const tagToAdd = await api.tags.read({slug: tag});

        console.log('Found tag', tagToAdd);

        // Admin API automatically includes tags and authors
        // WARNING: If the site is really big (1000s of posts) maybe do this paginated
        const allPosts = await api.posts.browse({limit: 'all'});

        // convert our list of posts, to a list of promises for requests to the api
        const result = await Promise.mapSeries(allPosts, async (post) => {
            // Add the tag to the post
            post.tags.push(tagToAdd);

            // Weird.. we shouldn't need to do this:
            delete post.uuid;
            delete post.comment_id;

            console.log('Updating', post.slug);
            // Call the API
            let result = await api.posts.edit(post);

            // Add a delay but return the original result
            return Promise.delay(50).return(result);
        });

        console.log(`Updated ${result.length} posts`);
    } catch (err) {
        console.error('There was an error', require('util').inspect(err, false, null));
        process.exit(1);
    }
}());

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![naz](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/naz/32/30666_2.png) [@naz](https://forum.ghost.org/u/naz)\
**Post date:** [May 13, 2021, 10:55am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/23 "2021-05-13T10:55:22Z")

</div>

Hey @thebear.dev ! These files are, as the repository name suggests - “demos”. They were meant to solve a problem someone once had when dealing with bulk edits, so there are no “recipes” around there. I’d find a script that’s the closest to what you are trying to do and modify it a little 😉

---

<div class="post-metadata">

**Author:** ![thebear.dev](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/thebear.dev/32/13956_2.png) [@thebear.dev](https://forum.ghost.org/u/thebear.dev)\
**Post date:** [May 13, 2021, 10:58am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/24 "2021-05-13T10:58:46Z")

</div>

Yes, which is what I have done. I’m just not sure how to modify the **`add-tag-to-all-posts.js`** script to bulk remove a tag.

If you can help with that, I would appreciate it. If not, I’ll ask around for help. If there’s a way that I missed to bulk remove a specific tag from multiple posts from the Ghost admin - please let me know.

I am asking since we now can choose to turn the portal on / off. If turning it off on a site that has multiple posts tagged as paying members only, the posts are still locked after turning the portal off.

---

<div class="post-metadata">

**Author:** ![thebear.dev](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/thebear.dev/32/13956_2.png) [@thebear.dev](https://forum.ghost.org/u/thebear.dev)\
**Post date:** [May 13, 2021, 11:00am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/25 "2021-05-13T11:00:55Z")

</div>

Crap… sorry. What I mean is turning the post access into Public, not bulk removing tags.

---

<div class="post-metadata">

**Author:** ![Kevin](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/kevin/32/32_2.png) [@Kevin](https://forum.ghost.org/u/Kevin)\
**Post date:** [May 13, 2021, 12:43pm UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/26 "2021-05-13T12:43:44Z")

</div>

You can use the same `find-and-replace` script you used previously. To make all posts public you’d change

```auto
post.mobiledoc = post.mobiledoc.replace(/hello_/gmi, 'goodbye_');

```

to

```auto
post.visibility = 'public';

```

---

<div class="post-metadata">

**Author:** ![thebear.dev](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/thebear.dev/32/13956_2.png) [@thebear.dev](https://forum.ghost.org/u/thebear.dev)\
**Post date:** [May 16, 2021, 6:31am UTC](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549/27 "2021-05-16T06:31:44Z")

</div>

Thank you. All sorted now.

[Previous page](https://forum.ghost.org/t/replace-post-content-using-sql-query/22549.md?page=1)
