Make/create a category for every page

I’m trying to make it so there’s a category that matches every page (excluding a couple).

So if I have the pages as such:

Pages

|– Ninja Turtles
| |–Turtles
| | |–Raphael
| | |–Leonardo
| | |–Donatello
| | |–Michelangelo
| |
| |–Other
| | |–Splinter
| | |–Shredder
|
|– Painters
| |–Rococo
| | |–Jean-Antoine Watteau
| | |–Francois Boucher
|
| |–Renaissance
| | |–Raphael
| | |–Donatello
| | |–Michelangelo

Then I wanted to have categories with the exact same structure (also child/parent relations):

My plan is/was to make a function in functions.php and hook it onto the save_post-hook. So every time a page is saved, then it’ll go through all pages and see if a category existed that had that name, – and if it didn’t then create it. But I’ve encountered two problems:

There’s no built-in function called get_category_by_name() (only _by_slug, _by_path or _by_id).
As can be seen on the page-tree above, then it needs to check along it’s siblings only (since there’s two pages called Raphael, with two different meanings). So every time I have a page, then I have to find a category that matches by title (somehow), and then check that the category’s parent’s title, matches the pages’ parent’s title. And in order to make the function perfect, then I have to do that all the way to the root-categories/pages.

So this quickly escalates in difficulty.

Are there a better way (more light-weight-way) of achieving this?

Read more here:: Make/create a category for every page

Leave a Reply

Your email address will not be published. Required fields are marked *