title: Dynamic URLs in WordPress
body: <p><strong>Background</strong></p>
  
  <p>I'm building a course catalog for a school and am trying to make friendly, dynamic URLs. It has three custom post types:</p>
  
  <ul>
  <li>catalog.</li>
  <li>course. This post type contains a title, course number, and course description. </li>
  <li>section. This post type contains a cost, start and end dates and times, etc. A section is an instance of a course; for example, there might be two sections of Drawing for Beginners, one on Thursday morning and one on Monday night and taught by two different faculty.</li>
  </ul>
  
  <p>There is a one-to-many relationship between course and section. When viewing a section, it displays the parent course's course name, course number, and course description. It also displays that section's cost, start and end dates and times, etc. </p>
  
  <p>The current URL for a section is this:</p>
  
  <p><a href="http://website.org/section/section-slug/" rel="nofollow noreferrer">http://website.org/section/section-slug/</a></p>
  
  <p>I would like it to be this:</p>
  
  <p><a href="http://website.org/class/spring-2019/drawing-for-beginners/01/" rel="nofollow noreferrer">http://website.org/class/spring-2019/drawing-for-beginners/01/</a></p>
  
  <p>...where "class" is a 'virtual' folder / synthetic prefix
  ...where "spring-2019" corresponds to a "catalog" custom post type
  ...where "drawing-for-beginners" corresponds to a slug for the parent "course" custom post type
  ...where "01" corresponds to the section of the course.</p>
  
  <p>WordPress should display the section post type that matches these criteria.</p>
  
  <p><strong>Research</strong></p>
  
  <p>I've read quite a bit about both the Rewrite API and the Endpoints API. The best article I could find on a topic closest to what I'm trying to accomplish is <a href="https://roots.io/routing-wp-requests/" rel="nofollow noreferrer">this one</a>. Sadly, the example code on the page didn't work for me, resulting in a "too many redirects error." I can provide more details about exactly which portion didn't work, if desired. I also read <a href="https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/" rel="nofollow noreferrer">this article on Make WordPress Plugins</a> but it doesn't cover dynamic lookups.</p>
  
  <p><strong>What I'm trying to accomplish</strong></p>
  
  <p><a href="http://website.org/class/spring-2019/drawing-for-beginners/01/" rel="nofollow noreferrer">http://website.org/class/spring-2019/drawing-for-beginners/01/</a></p>
  
  <p>When the virtual URL is supplied to WordPress, I need the system to perform a query to look up the section whose number is "01" and which belongs to the "spring-2019" catalog and whose parent course has a slug of "drawing-for-beginners." That query isn't a challenge, it's all of the hooks that need to be called to execute that code and return the correct template (for the section page). I'm not sure whether I need to create an Endpoint or can just get away with adding rewrite rules.</p>
  
  <p>Can someone offer some guidance?</p>
  
  <p>--</p>
  
  <p>As a "bonus," I'm also trying to accomplish this:</p>
  
  <p><a href="http://website.org/class/spring-2019/drawing-for-beginners/01/faculty_name" rel="nofollow noreferrer">http://website.org/class/spring-2019/drawing-for-beginners/01/faculty_name</a></p>
  
  <p>...where "faculty_name" is dynamic and is the name of the person teaching that section (and corresponds to a "faculty" custom post type).</p>
  
  <p><a href="http://website.org/class/spring-2019/drawing-for-beginners/01/gallery" rel="nofollow noreferrer">http://website.org/class/spring-2019/drawing-for-beginners/01/gallery</a></p>
  
  <p>...where "gallery" is a static name and shows a gallery custom post type.</p>