Rewrite CPT child page URL to support query var

Okay, after a lot of playing around, I found this:

  1. The regex it was matching (while skipping over my rule) was practice-area/(.+?)(?:/([0-9]+))?/?$
    • This had a $matches array of 2: parent-page/child-page and a number that follows that, which is blank since there isn’t one in the URL I was trying to build.
    • Also the URL it was redirecting to was index.php?post-type=$matches[1]&page=$matches[2]. I didn’t realize that post-type could be a query var key. So that’s what ultimately got me on the right path.
  2. So I decided to directly copy that, but instead of looking for a number I look for any character
    • I used post-type/(.+?)(?:/(.+))?/?$
    • This didn’t work because I ended up with $matches[1] = parent-page and $matches[2] = child-page/section-id
  3. So, with a bit more tweaking, and messing with the laziness of the capture groups, I came up with post-type/(.+)/(.+?)/?$, which successfully superseded the regex in (1) above.
    • $matches[1] = parent-page/child-age and $matches[2] = section-id

The final rewrite rule is:

add_rewrite_rule('post-type/(.+)/(.+?)/?$', 'index.php?post-type=$matches[1]&section=$matches[2]','top');