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
    Tips
    New wp-config Tweaks You Probably Don't Know8 new wp config tweaks you probably didnt know about 400
    The wp-config.php file: One of the most loved WordPress feature for some, one of the worst nightmares for others. There are countless tips and tricks in a plethora of articles, and you can't get enough of them; however, this article is aiming to be different. In this article, we're going to get familiar with eight new wp-config tricks that are less known than functionality such as turning off post revisions, increasing the memory limit, or other similar features.Read More…
  • Code
    Theme Development
    Getting The Most of Post Formats: What Are Post Formats?Postformats
    In this series, I will be introducing you to WordPress post formats, dispelling some of the confusion surrounding them and showing you how to put theme to use in your themes. Post formats may be misunderstood, and have a misleading name, but they are still one of the more powerful tools at a theme developer's disposal. Once you understand post formats you will be able to use them to ensure that all of the different types of posts on a site that your theme is used on display properly, while avoiding using unneeded custom post types that limit data portability.Read More…
  • 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
    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
    Creative Coding
    Using WordPress for Web Application Development: Features: Saving DataApplication foundation 400
    When it comes to the web, almost any application that runs within your browser is backed by a data store of some type. Usually, these data stores are in the form of a type of database. Until recently, most applications were built on top of some type of SQL database, but with the rise of document-based databases such as CouchDB, other applications have begun to use other backends as well.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…