Ghost-admin to update the custom template on all posts

I’m trying to update all my old posts to shed my custom Blogger template now that Google+ comments are getting killed.

I tried getting this done through the Ghost-admin API, but I can’t seem to get a valid edit done. Is there a way to just patch a single field?

const GhostAdminAPI = require('@tryghost/admin-api');

const api = new GhostAdminAPI({
    url: 'https://scrumbug.ghost.io',
    key: 'xxxx',
    version: 'v2'
  });

api.posts.browse({limit: 400}).then(
    posts => {
        for(var post in posts) {
            console.log(posts[post].id);
            ...
        }
    }
).then( promises => Promise.all(promises) ).catch(e => console.error(e));

On the … I tried:

posts[post].custom_template = null;
return api.posts.edit( posts[post] );
 jhouwing  ~  node fix-content.js
5b18ec2e1b866300bfc10823
{ ValidationError: Validation error, cannot edit post.
    at makeRequest.catch (/home/jhouwing/node_modules/@tryghost/admin-api/lib/index.js:290:33)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
  name: 'ValidationError',
  context: "Validation failed for 'posts[0]'",
  type: 'ValidationError',
  details:
   [ { keyword: 'additionalProperties',
       dataPath: '.posts[0]',
       schemaPath: '#/additionalProperties',
       params: [Object],
       message: 'should NOT have additional properties' },
     { keyword: 'additionalProperties',
       dataPath: '.posts[0]',
       schemaPath: '#/additionalProperties',
       params: [Object],
       message: 'should NOT have additional properties' } ],
  property: 'posts[0]',
  help: null,
  code: null,
  id: '11848300-49c6-11e9-a986-c7968b6671f1' }

and

return api.posts.edit( { id: posts[post].id, custom_template: null } );
 jhouwing  ~  node fix-content.js
5b18ec2e1b866300bfc10823
{ ValidationError: Validation error, cannot edit post.
    at makeRequest.catch (/home/jhouwing/node_modules/@tryghost/admin-api/lib/index.js:290:33)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
  name: 'ValidationError',
  context: "Validation failed for 'posts[0]'",
  type: 'ValidationError',
  details:
   [ { keyword: 'required',
       dataPath: '.posts[0]',
       schemaPath: '#/properties/posts/items/required',
       params: [Object],
       message: "should have required property 'updated_at'" } ],
  property: 'posts[0]',
  help: null,
  code: null,
  id: 'd7845630-49c5-11e9-a986-c7968b6671f1' }

Neither works and the errors I get scare me a bit in that I may accidentally lose old markdown contents this way.

Hey @Jesse_Houwing :wave: My understanding is you are trying to update a single field on a post custom_template. You almost got it with the last try :slight_smile: The validation error you received says you would need to provide updated_at fields to do the update (it’s needed for collision detection in case somebody else is editing same post). With a bit modified syntax, your code would look something like:

return Promise.all(posts.map(post => return api.posts.edit({
  id: post.id,
  custom_template: null,
  updated_at: post.updated_at
})))

Let me know if that worked :+1: And please backup your content and possibly verify on local instance first to avoid any unintended edits :wink:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.