I figured this all out after much research.
I hope this helps others!
The Dummies Guide to Adding Categories and Tags with the WordPress Create a Post Module
When you publish posts to WordPress, you’ll want to add categories and tags to your posts. This can be straightforward when you’re dealing with just one category or tag. However, if you want to add multiple categories or tags at once, you’ll need to format the data correctly as an array of IDs. This short guide walks you through the process step-by-step, starting from the simple case and moving to multiple tags or categories.
The Simple Case: One Category or Tag
If you only need to assign a single category or tag, WordPress expects a numerical ID.
For example, if you have one category with an ID of 225
, you can simply put 225
into the WordPress “Category ID” field. The same goes for a single tag—just provide the tag’s ID as a single number.
- Example:
Categories: 225
Tags: 320
In this simple scenario, no special formatting or JSON manipulation is needed.
When You Have Multiple Categories or Tags
WordPress’s API requires that multiple categories or tags be sent as an array of IDs. For example, if you want to assign categories 225
, 158
, and 159
, you must send them as [225, 158, 159]
. The same goes for tags: [320, 293]
.
The Problem: If you’re pulling this data from a source like Google Sheets, you may have them stored as a plain string, such as "225,158,159"
. This isn’t an array—just a string. Additionally, you cant just add [225, 158, 159]
. To make WordPress happy, you need to convert it into an array.
Converting Your Data into an Array
To convert your string into a proper array of IDs for WordPress, use the “Parse JSON” module.
- Format Your Source Data as JSON:
Instead of just putting "320,293"
in your sheet, store it formatted as a JSON array of objects:
{"tags":[{"id":320},{"id":293}]}
For categories:
{"categories":[{"id":225},{"id":158},{"id":159}]}
By using this format in your source data, i.e. wrapping your IDs in an array of objects (each with an id
field) and placing them under a top-level key like tags
or categories
, you create a structure that can be parsed.
- Use the Parse JSON Module:
- After your Google Sheets step, add a “JSON > Parse JSON” module.
-
In the “JSON string” field, select the cell value from your Google Sheets output.
-
Click the option to “Generate Schema from Sample” and paste your sample JSON (e.g., {"tags":[{"id":320},{"id":293}]}
).
-
Save the schema. The Parse JSON module will now output a structured array that you can use in the WordPress module.
- Combining Tags and Categories Together:
If you want to handle both categories and tags in one go, just include both in the same source data (in your Google sheet for example) as JSON:
{
"categories": [{"id":225},{"id":158},{"id":159}],
"tags": [{"id":320},{"id":293}]
}
Now you have a single Parse JSON module output that contains both a categories
array and a tags
array.
Using the Parsed Output in the WordPress Module
After the Parse JSON step Tags and Categories are formatted correctly.
To pass them into the WordPress Create a Post module, do the following:
- In the WordPress module’s Categories or Tags field, use the
map()
function to extract the IDs:
{{map(x.categories; "id")}}
{{map(x.tags; "id")}}
Replace x
with the Parse JSON module’s step number.
Now the tags and categories are IDs in an array, which WordPress expects. For example, my Parse JSON is step 13, so {{map(13.tags; "id")}}
will return [320, 293]
.
Now you’ve got neatly formatted arrays of IDs for both categories and tags that the WordPress module can accept.
In Summary
- One category/tag: Just use the numeric ID directly.
- Multiple categories/tags: Format them as a JSON object containing arrays of objects, use the Parse JSON module, and then map the result to just the IDs.
- Combined data: Put both categories and tags into one JSON object and parse them together, then reference each array separately in your WordPress module.
With this approach, you can easily assign multiple categories and tags to new WordPress posts even when pulling data from a source like Google Sheets.