Search code examples
phpwordpresscustom-post-type

How do I create custom taxonomy as a custom post type?


I want to create a category "University" for the post type "Courses". However, I also want to not just keep university as a category but create a custom post type in order to display the detailed university information.

I have successfully created "university" post type and "courses" post type with categories.

<?php

    function my_plugin() {

        // Courses Post type with University as a category
        register_post_type( 'Courses', array(
            'public' => true,
            'taxonomies'  => array( 'category' ),
        ));

        // University Post type
        register_post_type( 'university', array(
            'public' => true,
            'taxonomies'  => array( 'category' ),
        ));
    }

    add_action( 'init', 'my_plugin' );

?>

Somehow, I cannot relate custom post type "university" with "Courses" such that it appears as a category of courses as well as a CPT itself.


Solution

  • That's not possible in WordPress. An object can only be a post type or a taxonomy, not both.

    A solution would be to create both the post type and the taxonomy, and then create your own database table to save the relations between both. A more simple solution (but depending on the way you need to get the relations) would be to use Advanced Custom Fields, and use that to link them together.