X

Custom Post Types And Custom Taxonomies in WordPress


Custom Post Types And Custom Taxonomies in WordPress. Here I am going to explain both things, creation of  Custom Post types and  Taxanomies(Categories). With Help of few Sample codes. First we are going to create Custom Post types for Sales Items(Just for example).

Untitled-1

Create Custom Post Types: 

First we go through the syntax of Custom Post creation.

<?php register_post_type( $post_type, $args ); ?>

Here $post_type stands our new custom post type and $args stands the required arguments for our new post types . 

Before getting into custom post creation remember the default post types.

  • post
  • page
  • attachment
  • revision nav_menu_item

Now we are going to create a new post type by the following code.

register_post_type( 'sales', array(
					'labels' => array(
					'name' => 'Sales',
					'singular_name' => 'sales',
					'add_new' => 'Add New',
					'add_new_item' => 'Add New Sales Pro',
					'edit' => 'Edit',
					'edit_item' => 'Edit Sales Pro',
					'new_item' => 'New Sales Pro',
					'view' => 'View',
					'view_item' => 'View Sales Pro',
					'search_items' => 'Search Sales Pros',
					'not_found' => 'No Sales Pros found',
					'not_found_in_trash' =>
					'No Sales Pros found in Trash',
					'parent' => 'Parent Sales Pro'
					),
				'public' => true,
				'menu_position' => 4,
				'supports' => array( 'title', 'editor', 'comments', 'thumbnail','excerpt','custom-fields','comments'), 
				'taxonomies' => array( 'os','language' ), 'menu_icon' => get_bloginfo('template_url') . '/images/sales_pro.png','has_archive' => true )
			);

The above code helps you to create a new post “Sales” , We passed arguments in a array. first, Label help you to specify the required fields label, I mean the label for your menu item and edit items, if you use language translations, it will help you more for language translations.

By enabling Public as true the post type available for public use, i.e You can use it as public posr\t, by the way you can create private post by making it as fales.

Menu position helps you to place the menu item in a order.

supports, is for your posts custom features like right side panel of  ” Publish, Categories, Tags, and Featured image” , you can add it after the declaration of new post type by the following function

 add_post_type_support() . We discus it later, by the way you can add and remove post suport with the help of  remove_post_type_support(). 

taxonomies , this will help you to add custom taxonomies for your new Custom post type.

Create Custom Taxonomies :

Here , first I will give you the sample code and explain you ,

register_taxonomy( 'os', array('sales','purchase'), 
						array('hierarchical' => true,  'labels' => 
							array(
                            				'name' => __( 'OS Categories', 'appthemes'),
                            				'singular_name' => __( 'OS Category', 'appthemes'),
                            				'search_items' =>  __( 'Search OS Categories', 'appthemes'),
                            				'all_items' => __( 'All OS Categories', 'appthemes'),
                            				'parent_item' => __( 'Parent OS Category', 'appthemes'),
                            				'parent_item_colon' => __( 'Parent OS Category:', 'appthemes'),
                            				'edit_item' => __( 'Edit OS Category', 'appthemes'),
                            				'update_item' => __( 'Update OS Category', 'appthemes'),
                            				'add_new_item' => __( 'Add New OS Category', 'appthemes'),
                            				'new_item_name' => __( 'New OS', 'appthemes'),
								'menu_name'         => __( 'Operating System' )
                    ),
                    'show_ui' => true,
                    'query_var' => true,
			'update_count_callback' => '_update_post_term_count',
                    'rewrite' => array( 'slug' =>'os', 'hierarchical' => true ),
            )
    );

	$oss = array(
			'Windows',
			'Linux',
			'Mac',
			'Android',
			'iOS'
		);
	if ($oss) foreach($oss as $os) {
		if (!get_term_by( 'slug', sanitize_title($os), 'os')) {
			wp_insert_term($os, 'os');
		}
	}

From my, above example, I just wrote “os”  on the taxanomy , here is the syntax for register a new taxonomy,

<?php register_taxonomy( $taxonomy, $object_type, $args ); ?>

the taxonomy name followed by the supported post types, you can give it as array or single item,  than taxonomy parameters in a array. Just like the custom post registration many custom fields are there to fill up,

and below that, show_ui helps you to render the UI. This how you can create custom post type and custom taxonomy .

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, OpenCart Theme Development, PHP, HTML, CSS, jQuery, Bootstrap development and content SEO.

Comment Below

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

*

Current ye@r *

Menu

Sidebar

Subscribe E-mail Updates

Subscribe to our email newsletter.

 

Advertisement

Category Posts