Skip to content

Instantly share code, notes, and snippets.

@pujianto
Created November 23, 2019 07:36
Show Gist options
  • Save pujianto/7c8cafe871f74716d6369d859a82f0f1 to your computer and use it in GitHub Desktop.
Save pujianto/7c8cafe871f74716d6369d859a82f0f1 to your computer and use it in GitHub Desktop.
wpapi.js Get or Create Tag example
const api = require('wpapi');
const wp = new api({
endpoint: 'https://wordpress.domain/wp-json',
username: 'admin',
password: 'admin'
});
var tags = ['a','b','c'];
var getOrCreateTags = tags.map(tag => {
return wp.tags().slug(tag)
.then(r => {
return r.length ? r[0]: wp.tags().create({name: tag, slug: tag});
});
});
Promise.all(getOrCreateTag).then(tagList => console.log(tagList)).catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment