Created
November 23, 2019 07:36
-
-
Save pujianto/7c8cafe871f74716d6369d859a82f0f1 to your computer and use it in GitHub Desktop.
wpapi.js Get or Create Tag example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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