Replace Post Content using SQL Query

Perfect! Thank you for the update :slight_smile:

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:

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$ 

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?

/**
 * 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!

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 :wink:

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.

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

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

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

to

post.visibility = 'public';
1 Like

Thank you. All sorted now.