Parent-child relationship in custom post type

Who doesn’t want to have an organized WordPress website? This is exactly the reason why custom post types are important. CTP allows us to separate data and easy access and update it when necessary. However, what enabled us to organize this content and give it hierarchy is the parent-child relationship in the custom post type. But, before we talk about the parent-child relationship, let’s start from the beginning.

Long-awaited CPT

If you don’t know why it is important to update your WordPress, here’s an obvious example. After the years of waiting, when WordPress 3.0 arrived, we were finally able to create custom post types. And this truly was the opportunity you wanted to know about as soon as possible. With it, a WordPress website was transformed from a simple blogging platform to a powerful Control Management System. Only with just a few lines of code, we are able to have our own type.

Before we get to the parent-child relationship in custom post type, let’s see first of all, what custom post type is.

What is CPT?

CPT of custom post types could be regarded as content. The term “post” could confuse you, as it’s not a blog post in any way. CPT can be any content you want, and there are no strict rules to define them.  Obviously, there are default and custom post types. Default post types are blog posts, pages and attachments, revisions, and navigation menus.

On the other hand, custom post types allow more creative control for creating, storing, and editing information.

You shouldn’t mix custom post types with blog posts. If you want to create structure and hierarchy for blog posts, you have categories and subcategories in WordPress at your disposal.

Use for custom post types

While there are WordPress default post types, some people need more than that. Here are some of the examples where custom post types are useful:

  • Event listings site, with event types and venues
  • Real estate listings
  • E-commerce sites with product options
  • Ticket system
  • Design gallery or a showcase

Keep in mind that these ideas are only some of the suggestions. There are a lot more ideas where you could use custom post types, so let your imagination run wild.

Parent-child relationship and posts

When you go to Settings and you enable the “Hierarchical” option, you can get your posts to behave more like pages, and you’ll be able to get parent/child relationships. For example, let’s say you had a post type that’s named “drink” and a parent page of “liquor”, and a child page of “vodka”.  When you viewed the “vodka” post, the URL would look something like this: yoursite.com/drink/liquor/vodka/

Basically, this will only affect your posts within your post type, not other post types that are related to each other. For example, the post type of “drink” would not be a child to a post type of “food”.

Limiting options

When speaking of post relationships, WordPress offers very little. Actually, the parent/child relationship in custom type posts is the only kind of relationship WordPress supports. However, it might seem challenging making a custom post type a parent and making another custom post type a child.

WordPress.
WordPress allows only parent-child relationship in the custom post type

Here’s an example from one project, where you can see different post types and assigning children posts to a parent post.

  • Custom post type named “Neighborhood”
  • For places within this neighborhood, we’ll have custom post type named “Places”
  • Keep in mind that these custom post types are non-hierarchical
  • The goal is to assign each place to a neighborhood, which will the parent post

Assigning a parent post

Hopefully, by now, you know a bit about post types and know how they work. However, in order to assign a parent post, you’ll need to do a bit of coding. In case you’re not familiar with shortcodes in WordPress, try to understand the basics. The basics should be enough, as you only need to add the following code to the plugin’s admin files in order to assign a parent post.

A code on laptop, necessary to make a parent child relationship in custom post type
Time for coding

For the sake of explanation, here we’ll use flat post-types Neighborhood and Place. And now, it’s time for coding:

/* Hook meta box to just the 'place' post type. */
add_action( 'add_meta_boxes_place', 'my_add_meta_boxes' );

/* Creates the meta box. */
function my_add_meta_boxes( $post ) {

	add_meta_box(
		'my-place-parent',
		__( 'Neighborhood', 'example-textdomain' ),
		'my_place_parent_meta_box',
		$post->post_type,
		'side',
		'core'
	);
}

/* Displays the meta box. */
function my_place_parent_meta_box( $post ) {

	$parents = get_posts(
		array(
			'post_type'   => 'neighborhood',
			'orderby'     => 'title',
			'order'       => 'ASC',
			'numberposts' => -1
		)
	);

	if ( !empty( $parents ) ) {

		echo '<select name="parent_id" class="widefat">'; // !Important! Don't change the 'parent_id' name attribute.

		foreach ( $parents as $parent ) {
			printf( '<option value="%s"%s>%s</option>', esc_attr( $parent->ID ), selected( $parent->ID, $post->post_parent, false ), esc_html( $parent->post_title ) );
		}

		echo '</select>';
	}
}

Don’t forget to change the post type names to your own post types. In order to understand the code better and use it on your website, here’s another example on GitHub that you might find useful.

Post relationships

While parent-child relationships in custom post types are useful, they’re also quite limited. If you want to have more complex solutions, you’ll have to look elsewhere. They are some plugins out there that can help you achieve similar effects of post relationships.

A mother with children and a tablet.
To give you a better picture

Why we need post relationships?

If we want to create connections between posts on a site, we’re basically using post relationships. And the reason why they’re so good is that they allow us to create advanced websites. Once we connect the custom post type posts with each other, we can avoid data duplication, and we ensure that all don’t need to edit a piece of information more than once. Also, since time is money, we need any method we can that can help us avoid mundane tasks.

Benefits of using parent-child relationship in custom post types

One of the obvious benefits of using the parent-child relationship in custom post type is that you can keep your site sorted. And search engines and users love a site with an organized structure. However, keep in mind that in order to really experience the benefits of this, you need to create a good plan and stick to it.

Newsletter

Stay Informed