How to Create Custom Post Types in WordPress

As you can see, we have added many more options to the custom post type with his code. It will add more features like support for revisions, featured image, custom fields, etc.

If you are working on a client site and do not want to install another plugin, then you can manually create your custom post type by adding the required code in your theme’s functions.php file or in a site-specific plugin (See: Custom Post Types Debate functions.php or Plugin).

function my_custom_posttypes() {
  $labels = array(
        'name' => 'Service',
        'singular_name' => 'Service',
        'menu_name' => 'Service',
        'name_admin_bar' => 'Service',
        'add_new' => 'Add New Service',
        'add_new_item' => 'Add New Service',
        'new_item' => 'New Service',
        'edit_item' => 'Edit Service',
        'view_item' => 'View Service',
        'all_items' => 'All Service',
        'search_items' => 'Search Service',
        'parent_item_colon' => 'Parent Service:',
        'not_found' => 'No Service found.',
        'not_found_in_trash' => 'No Service found in Trash.',
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_icon' => 'dashicons-admin-post',
        'query_var' => true,
        'rewrite' => array('slug' => 'service'),
	'publicly_queryable'  => false,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => 5,
        'supports' => array('title', 'editor', 'revisions', 'custom-fields', 'thumbnail')
    register_post_type('service', $args);

add_action('init', 'my_custom_posttypes');

Displaying Custom Post Types on The Front Page & Any Page

If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (Related: What is a Loop?).

In this code, first we have defined the post type and posts per page in the arguments for our new WP_Query class. After that we ran our query, retrieved the posts and displayed them inside the loop.


if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');

$query = new WP_Query( array( 'post_type' => 'service', 'posts_per_page' => 1, 'paged' => $paged ) );

if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div class="col-xl-3 col-sm-6 col-md-4 col-lg-3">
     <?php echo get_the_post_thumbnail( $page->ID, 'medium' ); ?>
        <div class="entry">
         <h3><?php the_title(); ?></h3>
         <p><?php the_excerpt(); ?></p>
          $char_limit = 95; //character limit
          $content = $post->post_content; //contents saved in a variable
          echo substr(strip_tags($content), 0, $char_limit);

          <a href="<?php the_permalink() ?>" class="btn-red">Read More</a>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>

We hope this article helped you learn how to create custom post types in WordPress.

If you liked this article, then please more WordPress Post view now. in Delhi NCR, in Laxmi nagar, in Ganesh nagar, in shakarpur, in Ramesh park, in Nirman Vihar, in Geeta colony, in Mandawali, in west vinod nagar, in karkardooma, in Preet vihar, in Shadipur, in Rajouri garden, in Bara khamba, in yamuna bank, in joshi colony, in Chandar vihar, in Manglam School, in Madhu vihar, in Mayur vihar, in Alla colony, in Dwarka, in Noida, in Greater Noida