Custom post types in WordPress are a great way to add and structure content to a website. Unlike a page however, custom post types cannot have a parent page assigned to them, at least not directly. This could be a major drawback if you want to have a custom post type nest under a specific page in menus and in the URL.

 

Why Add a Parent to a Custom Post Type

For example, if you have 2 pages, an about page and an employees page, the employees page can be nested under the about page to form a hierarchy for the website. This nesting is visible in the URL structure(www.mysite.com/about/employees), breadcrumbs, and navigation menus. Now we add the “staff” custom post type into the mix. The employees page contains a loop to display and paginate all of the staff posts, but clicking on a staff member reveals that the staff member single-staff page is rendered at www.mysite.com/staff/15 and not www.mysite.com/about/employees/15. The employees navigation menu item would not have a current or ancestor class to indicate you are at that area of the site, and any breadcrumbs plugins or functions will fail to show about or employees in the site hierarchy when on a single-staff page.

 

The Solution to Nesting a Custom Post Type Under a Page

To solve these issues we can do a few things. Let’s look at each issue individually.

 

Change a Custom Post Type URL

To change the url structure of a custom post type so that a single-staff page uses the www.mysite.com/about/employees/15 url we need to add an argument when we define the post type. If you are registering the post type manually add the ‘rewrite’ argument.

If you are using a plugin to create the custom post type then add a hook to your functions.php file.

To get this to work correctly you need to go to wp-admin and flush your permalinks.

 

Adding a Post Parent to a Custom Post Type

The next step is to add a parent to the custom post type. WordPress sadly is not reflecting this update in the URL structure or the nav menus, but it will help with breadcrumbs and any other places where your code or a plugin is referring to a post’s parent. There are two ways to add a parent to a custom post type. The first option is to add a metabox to allow users to select the parent page.

I defined the STAFF_PAGE_PARENT_ID constant for convenience, simply to default the parent to a certain page so user’s don’t need to select it each time they create a post type. That is not necessary if you do not know the page id that will be the parent page programmatically.

A second option to add a page as a custom post type parent is to simply store a page id to the custom post type any time one is created or saved. This option does require that you know the page id of the parent page in code.

 

Adding Menu Ancestors to Custom Post Types

The final step in this process is to add classes to ancestors of the custom post type in navigation menus. This allows parents to be styled to indicate they are current or active in the menus. I use the nav_menu_css_class filter to add css classes as needed.

This will work great if you have a single page parent and no depth to your page hierarchy. In our example we have two pages so if we want to add the ancestor classes to the about page in addition to the employees page then we need to get the parent(s) of the parent and recursively add classes up the hierarchy.

3 comments on “Setting a WordPress Custom Post Type Parent to a Specific Page

  1. Ryan Macdonald says:

    Awesome article! I had to change one of your functions a little bit because the array wasn’t correct but it got me going in the right direction! Thank you 🙂

    If you’d like me to send you my code, let me know.

  2. Roland Harald says:

    Thank you sir. Covers all tasks to be aware of.
    I had to rewrite the whole thing for our environment, but this gave me a lot of direction.

  3. Gerhard says:

    Interesting, it give some starting points, but in my opinion it’s poorly more a cheat then a real solution, because you use one predefined fixed page as parent and do it not the dynamical way. And what’s about more then one custom post type with different name?

Comments are closed.