
WordPress.com: Get Categories
This item gets categories on WordPress.com. When category names are specified (write one per line), the IDs of the categories will be saved. When no category name is specified, all categories will be retrieved and their names and IDs will be saved.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_Domain
- C1: Domain of the WordPress.com site *
- conf_Names
- C2: Data item that stores / to save category names *
- conf_Ids
- C3: Data item to save category IDs *
Notes
- This Auto Step is for WordPress.com (a service that hosts WordPress sites, provided by Automattic itself)
Capture

See Also
Script (click to open)
- An XML file that contains the code below is available to download
- wordpress-category-get.xml (C) Questetra, Inc. (MIT License)
- If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step
// This item requires no authentication.
const API_VERSION = 'v1.1';
const MAX_NUMBER = 1000; // 一度のリクエストで取得できる最大件数
const main = () => {
////// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
const domain = configs.get('conf_Domain');
const names = retrieveNamesAsList();
////// == 演算 / Calculating ==
let categories = getCategories(domain);
if (names.length > 0) { // 名前が指定されている場合は、その名前のカテゴリのみに絞る
categories = names.map(name => {
const category = categories.find(item => item.name === name);
if (category === undefined) {
throw new Error(`Category not found: ${name}`);
}
return category;
});
}
////// == ワークフローデータへの代入 / Data Updating ==
if (names.length === 0) {
saveData('conf_Names', categories.map(item => item.name).join('\n'));
}
saveData('conf_Ids', categories.map(item => item.ID).join('\n'));
};
/**
* config に設定された名前の一覧を読み出す
* @return {Array<String>} ids
*/
const retrieveNamesAsList = () => {
const dataDef = configs.getObject('conf_Names');
let dataStr = engine.findData(dataDef);
if (dataStr === null) {
return [];
}
// 末尾に改行がある場合、削除
if (dataStr.endsWith('\n')) {
dataStr = dataStr.slice(0, -1);
}
const dataArray = dataStr.split('\n');
// 残りの配列に空文字が含まれる場合、エラー
if (dataArray.some(item => item === '')) {
throw new Error('Empty lines are not allowed for category names.');
}
return dataArray;
};
/**
* カテゴリ一覧を取得する
* 一度のリクエストで取得しきれなかった場合、エラー
* @param {String} domain
* @return {Array<Object>} categories
*/
const getCategories = (domain) => {
const url = `https://public-api.wordpress.com/rest/${API_VERSION}/sites/${encodeURIComponent(domain)}/categories`;
const response = httpClient.begin()
.queryParam('fields', 'ID,name')
.queryParam('number', MAX_NUMBER.toString())
.get(url);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw new Error(`Failed to get categories. status: ${status}`);
}
const json = JSON.parse(responseStr);
if (json.found > MAX_NUMBER) {
throw new Error('Too many categories.');
}
return json.categories;
};
/**
* データ項目への保存
* @param configName
* @param data
*/
const saveData = (configName, data) => {
const def = configs.getObject(configName);
engine.setData(def, data);
};