Picture Text How to Create Custom Post Types in WordPress

How to Create a Custom Post Types in WordPress?

Custom post types are content types like posts and pages. By default, WordPress comes with these post types:

  • Post
  • Page
  • Attachment
  • Revision
  • Nav Menu

You can create your own custom post types and call them whatever you want.

For instance, if you run a company website, then you would probably want to create a services post type. This post type can have different custom fields and even its own custom category structure.

Let’s start,

// Our custom post type function
function webtut_posttype() {
 
    register_post_type( 'services',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Services' ),
                'singular_name' => __( 'Service' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'services'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'webtut_posttype' );

What this code does is that it registers a post type 'services' with an array of arguments. These arguments are the options of our custom post type. This array has two parts, the first part is labeled, which itself is an array. The second part contains other arguments like public visibility, has archive, slug, and show_in_rest enables block editor support.

Now let’s take a look at a detailed piece of code that can add more options to your custom post type.

/*
* Creating a function to create our CPT
*/
 
function webtut_posttype() {
 
    // Set UI labels for Custom Post Type
    $singular = 'Service';
    $plural   = 'Services';
    $labels = array(
        'name'                => _x( $plural, 'Post Type General Name', 'webtut' ),
        'singular_name'       => _x( $singular, 'Post Type Singular Name', 'webtut' ),
        'menu_name'           => __( $plural, 'webtut' ),
        'parent_item_colon'   => __( 'Parent ' . $singular, 'webtut' ),
        'all_items'           => __( 'All ' . $plural, 'webtut' ),
        'view_item'           => __( 'View ' . $singular, 'webtut' ),
        'add_new_item'        => __( 'Add New ' . $singular, 'webtut' ),
        'add_new'             => __( 'Add New', 'webtut' ),
        'edit_item'           => __( 'Edit ' . $singular, 'webtut' ),
        'update_item'         => __( 'Update ' . $singular, 'webtut' ),
        'search_items'        => __( 'Search ' . $singular, 'webtut' ),
        'not_found'           => __( 'Not Found', 'webtut' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'webtut' ),
    );
     
    // Set other options for Custom Post Type
     
    $args = array(
        'label'               => __( 'services', 'webtut' ),
        'description'         => __( 'Services We Provide', 'webtut' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
 
    );
     
    // Registering your Custom Post Type
    register_post_type( 'services', $args );
 
}
 
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
 
add_action( 'init', 'webtut_posttype', 0 );

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

WordPress comes with built-in support for displaying your custom post types. Once you have added a few items into your new custom post type, it is time to display them on your website.

Querying Custom Post Types

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?).

By querying the database, you can retrieve items from a custom post type.

<?php 
$args = array(
    'post_type' => 'services',
    'posts_per_page' => 10
);
$the_query = new WP_Query( $args ); 
?>
<?php if ( $the_query->have_posts() ) : ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <div class="entry-content">
            <?php the_content(); ?> 
        </div>
    <?php endwhile;
    wp_reset_postdata(); ?>

<?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

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.

Leave a Reply

%d bloggers like this: