Support » Developing with WordPress » Linking a Custom Post Type to a Custom Post Type

  • Resolved Quin



    I’ve created a few custom post types, and I need to add a feature so one custom post type can be linked to some others. Kinda of like the Posts and Categories, having a parent-child type of relationship.

    For example, I have user groups, user levels, and users. The users are assigned a level and a group. So you user “Adam” can be level 1 in the IT department, and he can be promoted up the levels, or be moved to a different department (say, Admin).

    The groups will have some custom posts, like IT and Admin, as will the levels (1 – 6).

    Any idea to create this type of relationship?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Ideally, you would manage everything under a single, hierarchical post type. Different post types are completely unrelated to each other by design. You are working against their very nature by attempting to relate them.

    That said, creating a relationship is not impossible. The approach depends on the anticipated relationships – one to one, one to many, many to one, just how many is “many”, and if reversing the relations is required. For example, you could setup a relationship table much like how taxonomy relationships are managed in the wp_term_relationships table. It’s essentially a list of post IDs and the corresponding terms. Instead of relating terms, you relate posts of other types.

    Depending on your needs, something even simpler may be used, like storing relations in post meta. Or something more complex is also possible if need be.

    Thread Starter Quin



    This is what I am aiming for.

    	$pro_area_array = array();
    	$pro_area_args = array(
    		'post_type' => 'proarea',
    		'publish_status' => 'publish',
    		'posts_per_page' => -1,
    		'order' => 'ASC'
    	$pro_area_query = new WP_Query($pro_area_args);
    	if($pro_area_query->have_posts()) {
    		$i = 0;
    		while($pro_area_query->have_posts()) {
    			$pro_area_array += array($i => get_the_title());
    	$pro_areas = array();
    	foreach ($pro_area_array as $key => $value) {
    		$pro_areas[] = $value;
    	$pro_areas = array(
    	echo '<span><label for="pro-area">Professional Area: </label></span>';
    	echo '<select name="pro-area">';
    	foreach($pro_areas as $pro_area) {
    		if($pro_area == get_post_meta($post->ID, 'pro-area', true))
    			echo '<option selected>'. $pro_area .'</option>';
    			echo '<option>'. $pro_area .'</option>';
    	echo '</select>';

    Now the dropdown is populated correctly, and using a “simple” array does as you expect, but whenever I try and use the actual array, the permalink is changed to the last pro-area.

    EDIT: As soon as I try to pul in the pro-area posts, it screws things over, changing the permalink and not saving/selecting the option.

    • This reply was modified 6 years, 1 month ago by Quin.
    Thread Starter Quin


    UPDATE: I have it fixed. The solution is here.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Linking a Custom Post Type to a Custom Post Type’ is closed to new replies.