HomeWORDPRESSWhat is the role of register_post_type() and register_taxonomy()?

What is the role of register_post_type() and register_taxonomy()?

1. register_post_type()

Role:

  • Registers a Custom Post Type (CPT) in WordPress.
  • Lets you create new content types beyond the default ones (posts, pages).

Example Use Case:

  • Creating a “Portfolio”, “Events”, or “Products” section in your site.

Basic Syntax:

function create_portfolio_post_type() {
    register_post_type( 'portfolio',
        array(
            'labels' => array(
                'name' => __( 'Portfolios' ),
                'singular_name' => __( 'Portfolio' )
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor', 'thumbnail' ),
        )
    );
}
add_action( 'init', 'create_portfolio_post_type' );

2. register_taxonomy()

Role:

  • Registers a Custom Taxonomy in WordPress.
  • Taxonomies group and organize content (similar to categories or tags).

Example Use Case:

  • Creating a taxonomy like “Genres” for a Movies CPT, or “Skills” for a Portfolio CPT.

Basic Syntax:

function create_genre_taxonomy() {
    register_taxonomy(
        'genre',
        'movies',
        array(
            'label' => __( 'Genres' ),
            'hierarchical' => true, // true = category-like, false = tag-like
        )
    );
}
add_action( 'init', 'create_genre_taxonomy' );

Key Difference

FunctionPurpose
register_post_type()Defines what kind of content you store.
register_taxonomy()Defines how you group or classify that content.

💡 Analogy:
If your website is a library:

  • Custom Post Type = “Books” shelf
  • Taxonomy = “Genres” to organize those books.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *