wordpress custom post types

How to Create Custom Post Types in WordPress?

How-To Guides

How to Create Custom Post Types in WordPress?

Hello reader,

Sometimes during the website development, you may face unexpected issues. For example, you purchased a WordPress theme at ThemeForest, successfully installed it, started to modify and realized that there is not enough functionality for your website. What to do in this case? First of all, no worries:) In this case, you can customize theme functionality.

I would like to tell about a few features.

Custom Post Type

As you may know, by default you can add only one post type. Buy what to do if you want to add more than one post type? Just create your own post type.

Here you can find a documentation https://codex.wordpress.org/Post_Types. For example, I want to add a custom post type which called “Books”:


 // common post type for books  
   if (!function_exists('libri_books_create_post_type')) :  
     function libri_books_create_post_type()  
       $labels = array(  
           'name' => 'Our books',  
           'singular_name' => 'book',  
           'add_new' => 'Add book',  
           'all_items' => 'All books',  
           'add_new_item' => 'Add book',  
           'edit_item' => 'Edit book',  
           'new_item' => 'New book',  
           'view_item' => 'View books',  
           'search_items' => 'Search books',  
           'not_found' => 'No books found',  
           'not_found_in_trash' => 'No books found in trash',  
           'parent_item_colon' => 'Parent books',  
       $args = array(  
           'labels' => $labels,  
           'public' => true,  
           'has_archive' => false,  
           'publicly_queryable' => true,  
           'query_var' => true,  
           'rewrite' => true,  
           'capability_type' => 'post',  
           'hierarchical' => false,  
           'supports' => array(  
           'menu_position' => 1,  
           'exclude_from_search' => true,  
           'menu_icon' => 'dashicons-book-alt'  
       register_post_type('libri_book', $args);  
     add_action('init', 'libri_books_create_post_type');  
   endif; // end of function_exists()  

Also, you can face the issue when the theme is using a page builder, but there are no elements which are necessary for your page. This can be easily solved by custom shortcodes. All you have to do is to modify your functions.php file and insert a new shortcode “[something_youwant]” (mine, in this case, is [random_book]) to the appropriate section of the page builder:

    * Shortcode(output random book at homepage)  
   add_shortcode('random_book', 'display_random_book');  
   function display_random_book($atts)  
     $query = new WP_Query(array(  
         'post_type' => 'libri_book',  
         'orderby' => 'rand',  
         'posts_per_page' => '1',  
     if ($query->have_posts()) { ?>  
       <div class="random-book">  
         <?php while ($query->have_posts()) : $query->the_post(); ?>  
           <h3 class="sc_title sc_title_regular sc_align_center" style="text-align:center;"><a  
                 href="<?php the_guid(); ?>"><?php the_title(); ?></a></h3>  
           <div class="content content_wrap">  
             <div class="book">  
               <div class="left">  
                 <div class="photo">  
                   <img src="<?php echo get_field("book_cover"); ?>"  
                        alt="<?php echo get_field("book_cover")["alt"]; ?>">  
               <div class="center">  
                 <div class="biography"><?php echo get_field('synopsis'); ?></div>  
                 <div class="button-wrap">  
                   <button><a href="<?php the_guid(); ?>">Read more</a></button>  
               <!--            -->  
               <div class="right">  
                 <div class="heading">What`s new?</div>  
                 <div class="items-wrap">  
                   <!--            -->  
                   <?php ob_start();  
                     $query2 = new WP_Query(array(  
                         'post_type' => 'libri_book',  
                         'orderby' => 'rand',  
                         'posts_per_page' => '5',  
                     )); ?>  
                   <?php if ($query2->have_posts()) { ?>  
                     <?php while ($query2->have_posts()) : $query2->the_post(); ?>  
                       <div class="item">  
                         <div class="cover" style="background-image: url('<?php echo get_field("book_cover"); ?>')">  
                         <div class="title"><a href="<?php the_guid(); ?>"><?php the_title(); ?></a></div>  
                     <?php endwhile;  
                   } ?>  
         <?php endwhile;  
           wp_reset_postdata(); ?>  
       <?php $myvariable = ob_get_clean();  
       return $myvariable;  
   } ?>  

Hope this small article will let you know that everything is possible!