themes

Complete Code to Register Custom Post Types and Custom Taxonomies in WordPress

In this example, we’ll create two custom post types Testimonials and Projects using the register_post_type() function. We’ll also register four custom taxonomies Type, Material, Service, and Category for the Projects post type using the register_taxonomy() function.

Simply add the following code to your theme’s functions.php file or a custom plugin. Once added, you’ll see the new post types and taxonomies in the WordPress admin dashboard, allowing you to organize and manage your content more efficiently.

<?php
/* Start Testimonials Function*/
function qrs_testimonial_posttype() {
    $labels = array(
            'name'               => _x( 'Testimonials', 'qrs' ),
            'singular_name'      => _x( 'Testimonial', 'qrs' ),
            'add_new'            => _x( 'Add New', 'qrs'),
            'add_new_item'       => __( 'Add New Testimonial' ),
            'edit_item'          => __( 'Edit Testimonial' ),
            'new_item'           => __( 'New Testimonial' ),
            'all_items'          => __( 'All Testimonials' ),
            'view_item'          => __( 'View Testimonial' ),
            'search_items'       => __( 'Search Testimonial' ),
            'not_found'          => __( 'No Testimonial found' ),
            'not_found_in_trash' => __( 'No Testimonials found in the Trash' ),
            'menu_name'          => 'Testimonials'
          ); 
    register_post_type( 'testimonials',

        array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => false,
            'supports'            => array( 'title','editor'),
            'rewrite' => array('slug' => 'testimonials'),
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'qrs_testimonial_posttype' );

/* End Testimonials Function*/

/* Start projects post type*/   
function qrs_projects_posttype() {
    $labels = array(
            'name'               => _x( 'Projects', 'qrs' ),
            'singular_name'      => _x( 'Projects', 'qrs' ),
            'add_new'            => _x( 'Add New', 'qrs' ),
            'add_new_item'       => __( 'Add New Project' ),
            'edit_item'          => __( 'Edit Project' ),
            'new_item'           => __( 'New Project' ),
            'all_items'          => __( 'All Projects' ),
            'view_item'          => __( 'View Projects' ),
            'search_items'       => __( 'Search Projects' ),
            'not_found'          => __( 'No Projects found' ),
            'not_found_in_trash' => __( 'No Projects found in the Trash' ),
            'menu_name'          => 'Projects'
          ); 
    register_post_type( 'Projects',
        array(
            'labels' => $labels,
            'public' => true,
            'has_archive' => false,
            'supports'    => array( 'title','editor','thumbnail'),
            'rewrite' => array('slug' => 'Projects'),
            'show_in_rest' => true,
        )
    );
     
    
}
add_action( 'init', 'qrs_projects_posttype' );

/* start register taxonomy for type*/
function qrs_type_taxonomies() {
    register_taxonomy('type', 'projects', array(
    'hierarchical' => true,
    'labels' => array(
      'name' => _x( 'Type', 'qrs' ),
      'singular_name' => _x( 'Type', 'qrs' ),
      'search_items' =>  __( 'Search Type' ),
      'all_items' => __( 'All Type' ),
      'parent_item' => __( 'Parent Type' ),
      'parent_item_colon' => __( 'Parent Type:' ),
      'edit_item' => __( 'Edit Type' ),
      'update_item' => __( 'Update Type' ),
      'add_new_item' => __( 'Add New Type' ),
      'new_item_name' => __( 'New Type Name' ),
      'menu_name' => __( 'Type' )
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'type',
      'with_front' => false,
      'hierarchical' => true 
    )
  ));
}
add_action( 'init', 'qrs_type_taxonomies', 0 );
/* End register taxonomy for type*/

/* start register taxonomy for Material*/
function qrs_material_taxonomies() {
    register_taxonomy('material', 'projects', array(
    'hierarchical' => true,
    'labels' => array(
      'name' => _x( 'Material', 'qrs' ),
      'singular_name' => _x( 'Material', 'qrs' ),
      'search_items' =>  __( 'Search Material' ),
      'all_items' => __( 'All Material' ),
      'parent_item' => __( 'Parent Material' ),
      'parent_item_colon' => __( 'Parent Material:' ),
      'edit_item' => __( 'Edit Material' ),
      'update_item' => __( 'Update Material' ),
      'add_new_item' => __( 'Add New Material' ),
      'new_item_name' => __( 'New Material Name' ),
      'menu_name' => __( 'Material' )
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'material',
      'with_front' => false,
      'hierarchical' => true 
    )
  ));
}
add_action( 'init', 'qrs_material_taxonomies', 0 );
/* End register taxonomy for Material*/

/* start register taxonomy for service*/
function qrs_service_taxonomies() {
    register_taxonomy('service', 'projects', array(
    'hierarchical' => true,
    'labels' => array(
      'name' => _x( 'Service', 'qrs' ),
      'singular_name' => _x( 'Service', 'qrs' ),
      'search_items' =>  __( 'Search Service' ),
      'all_items' => __( 'All Service' ),
      'parent_item' => __( 'Parent Service' ),
      'parent_item_colon' => __( 'Parent Service:' ),
      'edit_item' => __( 'Edit Service' ),
      'update_item' => __( 'Update Service' ),
      'add_new_item' => __( 'Add New Service' ),
      'new_item_name' => __( 'New Service Name' ),
      'menu_name' => __( 'Service' )
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'Service',
      'with_front' => false,
      'hierarchical' => true 
    )
  ));
}
add_action( 'init', 'qrs_service_taxonomies', 0 );
/* End register taxonomy for Service*/

/* start register taxonomy for Category*/
function qrs_category_taxonomies() {
    register_taxonomy('categories', 'projects', array(
    'hierarchical' => true,
    'labels' => array(
      'name' => _x( 'Category', 'qrs' ),
      'singular_name' => _x( 'Category', 'qrs' ),
      'search_items' =>  __( 'Category Category' ),
      'all_items' => __( 'All Category' ),
      'parent_item' => __( 'Parent Category' ),
      'parent_item_colon' => __( 'Parent Category:' ),
      'edit_item' => __( 'Edit Category' ),
      'update_item' => __( 'Update Category' ),
      'add_new_item' => __( 'Add New Category' ),
      'new_item_name' => __( 'New Category Name' ),
      'menu_name' => __( 'Category' )
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'slug' => 'category',
      'with_front' => false,
      'hierarchical' => true 
    )
  ));
}
add_action( 'init', 'qrs_category_taxonomies', 0 );
/* End register taxonomy for Category*/

/* End projects post type*/   

Leave a reply

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