简体   繁体   中英

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.

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.

As Sjors says, you can make a connection/relation between two CTP.

The ADC plugin allows it.

https://www.advancedcustomfields.com/resources/relationship/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM