Get started building your Woocommerce store or web application -- Call us today at 7868174424

Custom Post Type in WordPress (CPT)

WordPress is a wonderful CMS (Content Management System), currently a large percentage of websites are built on this platform. Its easy handling and low learning curve make users opt for this platform. But the question always arises of how to get the most out of WordPress, since this post will be directed to an important part of this technology, we will talk about CPT (Custom Post Type).

What are Custom Post Type?

Custom Post Type  in WordPress are just that, post types. We know that WordPress defines two important types of posts by default, post and page, and these are used to create posts in our blog, or to create the pages of our website. We can define a set of posts for our blog and they will be defined under certain categories that will classify the content.

Another important concept of WordPress are taxonomies, they are very linked with the CPT’s I recommend that you read this articles if you have not heard of them Taxonomies in WordPress. What are they for? How are they created?

Why do we need Custom Post Type?

The magic question would be this, what is the use of this? Well, it is very easy, the truth when WordPress arose was thinking to create blogs, that is, having a set of entries under a certain number of categories. But today this technology is used for any type of project, such as: e-commerce, a book website and even more. As they manage to create all types of content, it is just as simple, with the Custom Post Type. If we want our website to have a Portfolio, showing our work, we only have to create a Custom Post Type for it.

¿Comó crear un Custom Post Type en WordPress?

There are quick solutions like plugins that perform this task, for example Custom Post Type UI . But I always defend the idea of not using plugins for everything, that is why I leave you with some steps on how to create custom post type manually.

Always remember that the code must be in the functions.php file in your theme or the child theme (or inside of a custom plugin).

Step 1

The first step is to define a function, in my case it will be called wr_portfolio_cpt, to avoid collisions with other code it is always important to define the names of the functions with prefixes or suffixes, my theme is called Web Ready so my prefix is wr, and the CPT suffix referring to what the function defines, in this case Custom Post Type.

function wr_portfolio_cpt(){
     $labels = array(
         'name' => 'Portfolio',
         'singular_name' => 'Portfolio',
         'add_new' => 'Add Items',
         'all_items' => 'All Items',
         'add_new_item' => 'Add item',
         'edit_item' => 'Edit item',
         'new_item' => 'New Item',
         'view_item' => 'View Item',
         'search_item' => 'Search Portfolio',
         'not_found' => 'No items found',
         'not_found_in_trash' => 'No items found in trash',
         'parent_item_colon' => 'Parent Item'
     );

     $args = array(
         'labels' => $labels,
         'public' => true,
         'publicly_queryable' => true,
         'query_var' =>    true,
         'rewrite' => true,
         'capability_type' => 'post',
         'hierarchical' => false,
         'supports' => array(
             'title',
             'editor',
             'excerpt',
             'thumbnail',
             'revision'
         ),
         'taxonomies' => array('category','post_tag'),
         'menu_icon' => 'http://localhost/webready-remote/wp-content/uploads/2018/10/portfolio.png',
         'menu_position' => 5,
         'exclude_from_search' => false,
         'register_meta_box_cb' => 'wr_add_image_portfolio_metaboxes',
         'show_in_menu' => true,
         'has_archive' => true 
     );


     register_post_type('portfolio',$args);
 }

This function defines two variables ($labels, $args) and an internal function (register_post_type).

The variable $labels is an array that defines some properties that are used to define labels such as: the name with which the CPT will appear in the right menu in WordPress and other labels that you will see at the moment that you can customize.

The variable $arg is used as the second parameter of the function register_post_type(), it is like $ labels an array, and it already defines properties that are more functional like: the positions in the left menu in WordPress, or if you want to define a icon and even the elements you want this CPT to have.

Step 2

Define the hook that you created in a new CPT called portfolio.

add_action('init', 'wr_portfolio_cpt);

Conclusions

Custom Post Types are one of the key pieces of the WordPress core. Turn this CMS into a powerful tool for creating websites of all dimensions. Its use provides this platform with enormous versatility, since it does not limit it only to blogs, but to all types of projects. Currently this functionality makes WordPress have more followers and makes it the technology to follow right now. Please any questions write us or just leave a comment below.