Skip to content

Instantly share code, notes, and snippets.

@HoverBaum
Created March 26, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HoverBaum/70c19fe18ec6991e8a3070ba5a5257a2 to your computer and use it in GitHub Desktop.
Save HoverBaum/70c19fe18ec6991e8a3070ba5a5257a2 to your computer and use it in GitHub Desktop.
const getCategories = (posts, baseUrl, simpleLog = console.log) => new Promise(async resolve => {
const apiURL = `${baseUrl.replace(/\/$/, '')}/wp-json/wp/v2/categories`
// First reduce posts to an array of category numbers.
simpleLog('Reducing posts to category numbers')
const categories = await Promise.all(posts.reduce((all, post) => {
if(!post.category) return all
if(all.indexOf(post.category) > -1) return all
return all.concat([post.category])
}, [])
.map(async categoryNumber => {
simpleLog(`Getting information about categories`)
const categoryData = await getJSON(`${apiURL}/${categoryNumber}`)
return {
categoryNumber,
name: categoryData.name,
slug: categoryData.slug,
description: categoryData.description
}
}))
resolve(categories)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment