Nest pages under a parent and pick a page template

ContentIntermediate~10 min

Before you start

Plugin version 1.12.0 or newer. Update page is off until you switch it on, and Create page only if the parent does not exist yet (it needs publish_pages, or ask for a draft). The agent needs edit access to each page. Listing block templates needs the edit-theme-options capability; on a classic theme, tell the agent the template's file name instead. Pages owned by a page builder such as Avada or WPBakery are refused, so set their parent in the page editor by hand.

The services pages grew one at a time, so they all sit at the top level, and the URLs show it: /web-design/, /hosting/, /maintenance/ instead of living under /services/. Fixing it means opening each page, finding Page Attributes, choosing the parent and the template, and saving. With ten pages that is a slow afternoon.

This prompt does the same thing in one approved pass.

The prompt
I want these pages moved under our [Services] page: [Web design, Hosting,
Maintenance]. Each should use the [Full width] page template.
List the pages with get-pages and find the parent and the three child pages.
Tell me each page's id and current link, so we can see where it sits now. If
the Services page does not exist, ask me before creating it, as a draft.
Find the template's exact value: on a block theme, list the templates with
list-templates and give me its slug; on a classic theme, ask me for the file
name. update-page refuses a slug the theme does not offer for pages, so if
it is refused, ask me for the right one. Show me a table of each page, its current link and what will change, and
wait for my go.
Then update one page per call with update-page, sending only parent_id and
template. If a page is refused because a page builder owns it, skip it and
tell me. Read each page back with get-page and confirm its new link.

Why this is safe to run

The plan comes from reads. A page’s link shows where it sits in the hierarchy, so you see the before and after of every move in the table you approve. The update sends only the two fields that change, and the rest of the page stays as it is.

Both fields are checked before anything saves. A missing or trashed parent, a loop, or a template the theme does not offer is refused with nothing changed. Pages a page builder owns are refused for everything except an excerpt, since a write there could break the builder’s stored layout; the agent reports them instead of forcing the change.

Update page is off until you switch it on, is checked against the edit access of the user you bound the agent to, and each call lands in the Activity Log. Because it is covered by the Changes tab, each move can be reverted there while the page has not been edited since.

Moving a page under a parent changes its URL. Check the links and menu items that point at the old addresses afterwards, and add redirects where people or search engines still use them.

Already connected? Ask your agent for the “nest pages under a parent” recipe, name the parent, the pages and the template, and it runs the same flow on your own site.

How it works

  1. List the pages and find the parent and the pages to move under it.
  2. Find the template's exact slug or file name.
  3. Show each page's current link and its planned new place, and wait for approval.
  4. Update each page with parent_id and template, one page per call.
  5. Read each page back and confirm its new link.

Questions

Can a page end up under itself?
No. The parent must be a page that is not in the Trash, and it cannot be the page itself or one of its own sub-pages. A request like that is refused with nothing changed.
What counts as a valid template?
One the active theme offers for pages: a file name such as template-wide.php on a classic theme, or a template slug such as page-landing on a block theme, or default for the theme's default. Any other value is refused.
How do I undo a move?
Update page is one of the tools the Changes tab covers, and it records the parent and the template, so you can revert a page there while it has not been edited since. Sending parent_id 0 also takes a page back to the top level.