Exposing What and How About Custom Post Types in WordPress

tutorials, web development, wordpress 3462 Comments

WordPress has gained a great momentum as a fabulous Content Management System. The credit of its immense popularity can be given to its marvelous offerings that help it has an edge over its competitors. These features include resourceful plugins, themes, easy customizability, intuitive dashboard, open source nature and so forth.

Have you ever wondered what allows you to create a variety of content on WP powered websites? It is possible due to the integrated custom post type mechanism.

Custom Post Types in WordPress

Today, we will focus on one of the remarkable features of WordPress that is Custom Post Types. Let's delve deep into the topic and understand what it is and how to create it.

Custom Post Type

The way in which content is posted in WordPress is known as post type. There are five default post types that every WP site comprises. These include,

  1. Page - Akin to regular posts, but it is not accompanied to categories. The page post type usually possesses a few website pages and a specific navigation structure.
  2. Post - Post is a typical type that is used by most of the articles. This post type is also preferred for RSS feeds, and it displays in reverse sequential order.
  3. Revision - As the name signifies, this post type features drafts and any moderation in published posts. Revisions include unpublished posts.
  4. Attachment - It is used includes the info (like meta data, name, location, size, etc.) about an uploaded file.
  5. Navigation Menu - It showcases links to content in the menu structure to facilitate easy navigation.

The best thing about WP post types is that you are not restricted to use only these default post types, but you can also create a custom post type as per your needs. You can define custom parameters to it and use it in a desired fashion. The custom post types embrace certain styles and layouts, and thus, helps create a personalized look and feel as offered by page templates.

Here is a guide to learn how to create custom post types.

Customizing Your WP Site With Custom Post Types!

You can create custom post types by adding the appropriate lines of code in the functions.php file of the theme used in your WP site. It can be efficiently done by implementing the register_post_type() function. A new post type can be defined with a few attributes, including availability, supported features, and labels, to name a few.

Note: It is imperative to note that register_post_type() function must be called, after invoking the after_setup_theme action hook and before calling the admin_menu action hook. It is also advisable to use the init hook.

Let's understand the process with the help of an example. In the below code, I have created a post type - Articles, which is identified as custom_article.

Code Snippet:

add_action( 'init', 'custom_post_type' );
function custom_post_type() {
    register_post_type( 'custom_article',
        array(
            'labels' => array(
            'name' => __( 'Articles' ),
            'singular_name' => __( 'article' )
        ),
        'public' => true,
        'has_archive' => true,
        )
    );
}

You can add a list of arguments to the register_post_type() function. In the above code the two arguments are used, and each argument has a specific meaning.

  • Label is used to define the post type name.
  • Public is used to display the post type in the admin area of the website.

Similarly, you can use an argument to showcase the custom post types in search results, tweak their URLs, organize them in a hierarchy, and a lot more. You can review the list of supported arguments and choose a suitable for your website.

Naming Conventions - There are certain naming conventions that must be considered while naming the post types. As, it can be observed from the above example (custom_article is used as the name), the plugin, website or theme name is prefixed to the name. This convention helps avoid conflicts in other custom post type included in other themes or plugins. Also, ensure that the name should not exceed 20 characters.

Archives - It is essential to update the permalink structure so as to make your archive page easily accessible. All the templates for custom post types are saved in a similar fashion in the archives as the regular post types are saved.

archive-{post_type}.php

Templates - Custom templates can also be implemented for your custom post types. However, for this, you might need to adjust the permalink structure. For the above stated example, the file will implement the below mentioned line of code.

single-custom_article.php

Querying by Post Type - You can showcase posts from a particular post type by creating suitable queries, only after getting your template registered with your WordPress theme. This can be accomplished by implementing the post_type argument.

Code Snippet:

$args = array( 'post_type' => 'article', 'posts_per_page' => 7 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    echo '<h2>';
    the_title();
    echo '</h2>';
    echo '<div >';
    the_content();
    echo '</div>';
endwhile;

Custom Post Types in the Main Query - Once your custom post type is registered, you will still need to include it in the main query. You can showcase them in your archives and on the home page by implementing the pre_get_posts action hook.

Code Snippet:

// Show posts on home page of 'article', 'page' and 'product' post types
add_action( 'pre_get_posts', 'home_post_type_query' );
function home_post_type_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'article', 'page', 'product' ) );
    return $query;
}

Let's Wrap It Up

You can make your post stand out with a personalized visual appearance by using custom post type in your WordPress powered website. I hope this article has helped you understand the basic approach to manually create custom post types. However, one may also choose to install a suitable plugin for developing a desired custom post type with ease. Yes! There are several resourceful plugins like Custom Post Type Maker, Custom Post Type UI, and more available in the WordPress repository that augments the creation of custom post types.

themeforest banner
elegant themes banner

Related Articles:

You may be interested in:

My name is Andrey. I am a web developer in CMS Web Development Company. I am fond of finding simple solutions to complex problems. If you loved the topic, do share and comment. Also, you can follow me on Twitter for more updates.

Would you like to contribute to this site? Get started ยป
Rate this article:
(5.0 rating from 1 votes)

Comments