Custom post types


In WordPress, we have inbuilt post types like post, pages, attachments, revisions and changesets.

All of these post types are stored in the table posts. Wordpress allows us to create our own post types and also to extend its functionality like we can create a post type for movies etc.

To create a new custom post type we use register_post_type() function

function mlb_custom_post_type() {
	register_post_type('Movie',
	  array(
		'labels' => array(
			'name'=> __('Movies','textdomain'),
	'singular_name' => __('Movie','textdomain'),
			),
	);
}
add_action('init', 'mlb_custom_post_type');

This will register a new post type Movie which will be created at init hook.

Taxonomies

What are taxonomies in WordPress, it is just a way for classification of things like we use tags and categories.

We can aslo create our own custom taxonomies like for our movies we can categorize them in different genres

function mlb_register_taxonomy_genre()
{
    $labels = array(
        'name'=> _x( 'Genres', 'taxonomy general name' ),
		 'singular_name'=> _x( 'Genre', 'taxonomy singular name' ));

$args = array(
		 'hierarchical'   => false
		 'labels'         => $labels,
		 'show_ui'        => true,
		 'rewrite'        => [ 'slug' => 'genre' ],
	 );

Creating custom post types and taxonomies is the WordPress way of extending its functionality however we want without ever having to touch its core files.

Comments

Leave a Reply

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