Get all categories via API including subcategories

The /categories.json API call returns only top-level categories, at least on my server. I can read subcategories using /c/<id>.json but I don’t know how to discover those IDs.

3 Likes

To get all categories and subcategories from the API, you need to make a request to /site.json. If you don’t mind looking at PHP/WordPress code, there’s an example API request to get all categories here: https://github.com/discourse/wp-discourse/blob/master/lib/plugin-utilities.php#L104.

5 Likes

Terrific, thank you!

I notice that the category record returned doesn’t have a parent category. If it did, a flattened list from /catgories.json of all categories would be I think a less-surprising result, as opposed to only the root level.

1 Like

I see that subs have parent_category_id from sites.json. I wonder why then categories.json is only root level.

2 Likes

Note, you can now get all categories and sub-categories from categories.json with
/categories.json?include_subcategories=true

1 Like