What is WordPress Custom Post Types?
According to WordPress Codex, “Custom post types are new post types you can create. A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics.”
WordPress custom post types can perform along with WordPress’s default post types like Post, Page, Attachment etc. To create it, you need register post types. It depends on you how many types you need.
Why you need it?
Suppose your site is a portfolio website. You want to publish your works. Default post types are so cliche for the data input. That’s why WordPress let you create custom types. You can also create taxonomy, Tags, Category, Meta box as your wish.
How to create CPT?
First, add this code in functions.php
// register custom post type to work with
function portfolio_post_type() {
// set up labels
$labels = array(
'name' => 'Portfolios',
'singular_name' => 'Portfolio',
'add_new' => 'Add New Portfolio',
'add_new_item' => 'Add New Portfolio',
'edit_item' => 'Edit Portfolio',
'new_item' => 'New Portfolio',
'all_items' => 'All Portfolios',
'view_item' => 'View Portfolio',
'search_items' => 'Search Portfolio',
'not_found' => 'No Portfolios Found',
'not_found_in_trash' => 'No Portfolios found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Portfolios',
);
//register post type
register_post_type( 'portfolio', array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'supports' => array( 'title','thumbnail' ),
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'portfolios' ),
)
);
}
add_action( 'init', 'portfolio_post_type' );
This snippet means the initialization of post types. At first, you need to add labels i.e. “Add portfolio” “Edit portfolio” etc. It totally depends on you. After labeling, now you need to register your post types. In above codes, register_post_type() function allows you to define a new post type by its labels, supported features, availability and other specifics. We already defined the labels outside the function, though.
Now the Post Types options are showed in WordPress Dashboard.
Click on the “add new portfolio”, then you will see the custom post types with the fields. The supports argument ‘supports’ => array( ‘title’,’thumbnail’ ) will let you show what fields you want to show. In this example, I disabled WordPress editor.
You may notice that there are extra two fields “portfolio Type” and “portfolio link”. They are custom post types taxonomy. See below codes to how it works. The register_taxonomy function can take arguments to set label, slug most importantly.
add_action('init', 'portfolio_type');
function portfolio_type()
{
register_taxonomy(
'portfolio-type',
['portfolio'],
array(
'label' => __('Portfolio Type'),
'rewrite' => array('slug' => 'portfolio-type'),
'hierarchical' => false,
'show_tagcloud' => true,
)
);
}
add_action('init', 'portfolio_link');
function portfolio_link()
{
register_taxonomy(
'portfolio-link',
['portfolio'],
array(
'label' => __('Portfolio Link'),
'rewrite' => array('slug' => 'portfolio-link'),
'hierarchical' => false,
'show_tagcloud' => true,
)
);
}
To display taxonomy terms
function display_taxonomy_terms($post_type, $display = false) {
global $post;
$term_list = wp_get_post_terms($post->ID, $post_type, array('fields' => 'names'));
if($display == false) {
echo $term_list[0];
}elseif($display == 'return') {
return $term_list[0];
}
}
How to display custom post types in template?
Luckily, the WordPress theme system supports custom templates to display single posts belonging to a custom post type. Just like in the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively. Single posts of a custom post type will use single-{post_type}.php and their archives will use archive-{post_type}.php and if you don’t have this post type archive page you can pass BLOG_URL?post_type={post_type} . So, single-portfolios.php and archive-portfolios.php will be the name in this instance. You may wonder how to make the custom template for portfolio archives. Here you go,
First, open a text editor and paste these codes. Name it archive-portfolios.php
<pre>$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;</pre>
Then it will show 10 portfolio items. If you are using custom taxonomies with your post type and would like them to display in your template file use the_terms().
Now, let’s create single post templates for the custom post types. Name it single-portfolios.php. Generally, WordPress show single templates by default for default or custom post types. So just copy the default single.php loops. Hope you understand.
Well, this is it. Build your own custom post types. best of luck!
How to deregister custom post types?
Add these codes in functions.php
<?php
function delete_post_type(){
unregister_post_type( 'your_cpt_name' );
}
add_action('init','delete_post_type');
Just make sure you are using right custom post types name slug.
Well, you’ve learned about adding post types, showing it in a custom page or post and unregistering them easily. Let’s go for the big and complex practice.