WordPress

How To Create Custom Post Type(CPT) And Texonomy In WordPress

In this article, we will learn about how to create a Custom Post Type. Also, we will know how to add a Taxonomy of custom post type.

The post types are stored in the same database table(wp_posts) and differentiated by the post_type column.

By default, WordPress comes with Five post types are…

  • Posts
  • Pages
  • Attachments
  • Navigation menu items
  • Revisions
  • Links

To create custom post type there is a predefined function in wordpress.org register_post_type() using that function you can create CPT and it only is invoked through init action. whenever you create CPT always register taxonomy of that CPT. You can register taxonomy using register_taxonomy() function.

Syntax:

register_post_type( string $post_type, array|string $args = array() )

register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )

Open /wp-content/themes/theme_name/functions.php file and place the following code.

 /*Register a custom post type called "Blog"*/
function thecodehubs_cpt_blog_init() {
    $labels = array(
        'name'                  => _x( 'Blogs', 'Post type general name', 'textdomain' ),
        'singular_name'         => _x( 'Blog', 'Post type singular name', 'textdomain' ),
        'menu_name'             => _x( 'Blogs', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar'        => _x( 'Blog', 'Add New on Toolbar', 'textdomain' ),
        'add_new'               => __( 'Add New', 'textdomain' ),
        'add_new_item'          => __( 'Add New Blog', 'textdomain' ),
        'new_item'              => __( 'New Blog', 'textdomain' ),
        'edit_item'             => __( 'Edit Blog', 'textdomain' ),
        'view_item'             => __( 'View Blog', 'textdomain' ),
        'all_items'             => __( 'All Blogs', 'textdomain' ),
        'search_items'          => __( 'Search Blogs', 'textdomain' ),
        'parent_item_colon'     => __( 'Parent Blogs:', 'textdomain' ),
        'not_found'             => __( 'No Blogs found.', 'textdomain' ),
        'not_found_in_trash'    => __( 'No Blogs found in Trash.', 'textdomain' ),
        'featured_image'        => _x( 'Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'set_featured_image'    => _x( 'Set Featured image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'remove_featured_image' => _x( 'Remove Featured image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'use_featured_image'    => _x( 'Use as Featured image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
        'archives'              => _x( 'Blog archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
        'insert_into_item'      => _x( 'Insert into Blog', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
        'uploaded_to_this_item' => _x( 'Uploaded to this Blog', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
        'filter_items_list'     => _x( 'Filter blogs list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
        'items_list_navigation' => _x( 'Blogs list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
        'items_list'            => _x( 'Blogs list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
    );
     $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'blog' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    );
    register_post_type( 'blog', $args );
}
 
add_action( 'init', 'thecodehubs_cpt_blog_init' );

//Texonomy
function thecodehubs_texonomy_author_init()
{
    $labels = [
    'name'              => _x('Author', 'taxonomy general name'),
  'singular_name'     => _x('Author', 'taxonomy singular name'),
  'search_items'      => __('Search Author'),
  'all_items'         => __('All Author'),
  'parent_item'       => __('Parent Author'),
  'parent_item_colon' => __('Parent Author:'),
  'edit_item'         => __('Edit Author'),
  'update_item'       => __('Update Author'),
  'add_new_item'      => __('Add New Author'),
  'new_item_name'     => __('New Author Name'),
  'menu_name'         => __('Author'),
 ];
 $args = [
  'hierarchical'      => true, 
  'labels'            => $labels,
  'show_ui'           => true,
  'show_admin_column' => true,
  'query_var'         => true,
  'rewrite'           => ['slug' => 'Author'],
 ];
 register_taxonomy('Author', ['blog'], $args);
}
add_action('init', 'thecodehubs_texonomy_author_init');

register_post_type() Parameters:

  1. $post_type: (string)(Required) Post type key. May only contain lowercase alphanumeric characters, dashes, and underscores.
  2. $args: An array of arguments like…
  • label
  • show_in_admin_bar
  • menu_position
  • menu_icon
  • capability_type
  • capabilities
  • map_meta_cap
  • supports
  • register_meta_box_cb
  • taxonomies
  • has_archive
  • rewrite
  • can_export
  • _edit_link

register_taxonomy() Parameters:

  1. $taxonomy: (string) (Required) Name of the taxonomy.
  2. $object_type: (array|string)(Required) Name of the object type for the taxonomy.
  3. $args: An array of arguments like…
  • show_tagcloud
  • show_in_quick_edit
  • show_admin_column
  • meta_box_cb
  • meta_box_sanitize_cb
  • capabilities
  • rewrite
  • update_count_callback

register_post_type & register_taxonomy both function accept the following arguments in $args parameter.

  • labels
  • description
  • public
  • show_ui
  • show_in_nav_menus
  • show_in_menu
  • hierarchical
  • query_var
  • show_in_rest
  • rest_base
  • rest_controller_class
  • _builtin

After putting above code in function.php after that you will show it on admin side see below screenshot:

Get custom post type and taxonomy data on frontside

1. First, create a new file name like template-blog.php and place it in your theme folder like the below screenshot.

2. Put this code in the template file:

<?php
/*Template Name: Blog*/get_header();
$args = array( 'posts_per_page' => '-1', 'post_type' => 'blog' );
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) :  
     while( $the_query->have_posts() ) : $the_query->the_post(); ?>
           <p><?php _e( "Title:", "twentyseventeen-child" ) .the_title(); ?></p>
     <?php $terms = get_terms( array( 'taxonomy' => 'Author', 'orderby' => 'name' ) );
     foreach ( $terms as $term ) {
         echo  "Author: ".$term->name;
     } ?>
     <p><?php the_content(); ?></p>
     <?php endwhile; 
     wp_reset_postdata();
else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

3. Create a page and select a template so you can be show results as below screenshot.

4. Now you can see the custom post type and taxonomy data on the front side.

Hetal Rathod

Hetal Rathod is Wordpress Developer, and Author at thecodehubs.com.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago