Advertisement
Tips

Quick Tip: Hierarchical Custom Post Types Not Working for You?

by

Creating web sites can be challenging. Now a days, a typical web site can have many types of information to present to the reader. With a blog site, we mostly deal with posts that are added to the blog over time. But, if our site is about software, music, or almost anything else, it might be best to have a special way to separate the different types of information.

WordPress is a great Content Management System (or CMS) that allows us to define custom post types to better categorize what information we have for our readers. In creating web sites, I have used the custom post types in WordPress to create an FAQ sheet, a way to list PDF files, tutorials, and many other ways. Some plugins even use custom post types to create the dynamic content for a slider on the web site. There are many ways to make use of this very flexible system. But, it does have its problems: documentation!


The Problem

One feature of custom post types that I have had a very hard time figuring out is hierarchical post types. A hierarchical post type is a post type with a parent and one or more children. The pages post type in WordPress is a classic example of a hierarchical post type. One page can be the parent of other pages, that can be parents of other pages in a hierarchical fashion. This creates a very natural way to group many related pages.

While working on a new project that had a custom post type, it became very clear that having the custom post type hierarchical in nature similar to pages would be very beneficial. I dove into the documentation for custom post types. Most of the work done to make a custom post type is in the specification of the function for registering a post type: the register_post_type() function. This function is how we create custom post types in WordPress.

Scanning that document, I found this line:

hierarchical
(boolean) (optional) Whether the post type is hierarchical (e.g. page). Allows Parent to be specified.
Default: false

I thought, this is how we define a hierarchical post type. I set it to true and ran the script. I had my custom post type and everything looked okay. Then I noticed a problem. There was no meta-box area in the edit screen for designating the parent of the current custom post type. That line did not work!


The Solution

So, I did what every programmer does, I searched for it on Google! Surprisingly, I found many posts where people were asking this same question. But I never found a post that answered the question. When Google does not help, the only other solution: try every combination of arguments possible in the specification for the register_post_type() function! That took a while!

Finally, I noticed these lines:

supports
(array) (optional) An alias for calling add_post_type_support() directly.
Default: title and editor

* 'title'
* 'editor' (content)
* 'author'
* 'thumbnail' (featured image, current theme must also support post-thumbnails)
* 'excerpt'
* 'trackbacks'
* 'custom-fields'
* 'comments' (also will see comment count balloon on edit screen)
* 'revisions' (will store revisions)
* 'page-attributes' (menu order, hierarchical must be true to show Parent option)
* 'post-formats' add post formats, see Post Formats

I did not see anything that mentions hierarchical post types at first, but the words 'page-attributes' stood out to me. The only post type native to WordPress that is hierarchical is the page. Maybe if I gave my custom post page attributes, then maybe it would show the parent meta-box. Just as that thought went through my head, I finally noticed the end of the line that said 'hierarchical must be true to show Parent option'. Bingo! I knew that was what I needed. I added the line to my custom post type and sure enough, it worked. I now had a hierarchical custom post type!


Conclusion

Therefore, to make a hierarchical custom post type, you have to have the 'page-attributes' set in the 'supports' array and the 'hierarchical' flag set to true. I hope this was helpful to you as well!

What other little gotchas have you come across when developing for WordPress, and what are the tips you can share to save others the time? Share them around in the comments below.

Related Posts
  • Code
    Articles
    A Guide to Providing Quality Customer SupportQuality customer support
    If you’ve ever released free or premium WordPress Themes or Plugins, you know that launching your new product is not the end of the process. In fact, it’s just the start, and raises a lot of questions: How do you provide support? How do you support customers after they’ve used your product? How do you manage email, social media and forum support easily? Should you support your free products, or just your premium ones? Read More…
  • Code
    Creative Coding
    Advanced Use of Attachments in WordPress: Assigning Categories and Taxonomy Terms to AttachmentsAdvanced use of attachments in wordpress 400
    This tutorial is the first in a four part series in which you'll learn some techniques for working with images in attachments in WordPress which give you advanced options. Read More…
  • Code
    WordPress
    Quick Tip: Post Types, Taxonomies and PermalinksPost types taxonomies urls
    Custom Post Types and taxonomies are two powerful features of WordPress. Unfortunately, they can have a tendency to cause problems if developers aren't familiar with how permalinks, URLs, and rewriting works within WordPress. In this quick tip, we aim to cover the topic very briefly to make sure you know all you need to know about WordPress URLs, custom post types, taxonomies, and how they all relate.Read More…
  • Code
    Creative Coding
    Using a Custom Post Type to Create a Home Page BannerUsing a custom post type to create a home page banner
    Sometimes it's useful to have a nice obvious banner on your site's home page—for announcements, snippets that aren't long enough to merit a blog post, or links to new content within the site. But you don't want to be editing your homepage content every time you add a new piece of content, nor do you want to be delving into the code to add content.Read More…
  • Code
    Plugins
    The Beginner’s Guide to WordPress SEO by Yoast: Final TweakingThe beginners guide to wordpress seo by yoast
    In my previous article, I discussed the social settings of Yoast's WordPress SEO plugin. In this tutorial, you will learn the final steps to configuring the WordPress SEO plugin with the ultimate goal of making it as rock-solid as possible for your blog.Read More…
  • Code
    Plugins
    Create a Shortcode to List Posts With Multiple ParametersPost listing shortcode main image400
    On many of the client sites I build, I find there are times when I need to include a post listing on a page. I'm not talking about archive pages here, but adding a custom listing to an existing static page. For example, I might want to list some posts on the 'About' page, or the site may require an in-depth page for a topic, with a list of posts and custom post types related to that topic. One way to do this is by creating a custom page template to include the current page content plus the results of a second custom query, but if you want more flexibility over how you list posts, or just want to do it a few times, a shortcode will be a simpler solution. In this tutorial, I'll show you how to create a simple shortcode to list all posts of a custom post type, and then I'll expand on that to create a shortcode with various parameters that users can specify to list posts however they want.Read More…