pda-how-to-create-and-display-custom-post-types-in-wordpress

4 Easy Ways to Display Custom Post Type in WordPress

Custom post type plays an important role in organizing and grouping the content in WordPress. However, not everyone knows how to display a custom post type in WordPress after investing hours in creating it.

We have conducted research and accumulated possible methods that help you display your custom post type. You can save a lot of time by going through this article and singling out the most suitable method. So, what are you waiting for?

What Is a WordPress Custom Post Type?

In WordPress, Post Types refer to various kinds of content and they are kept in the wp_posts table. Each column in this table will contain a different kind of post type. These columns are called post_type.

Users can access and utilize default Post Types provided by WordPress. The most common ones are:

  • Page (‘page’)
  • Post (‘post’)
  • Revision (‘revision’)
  • Navigation menu (‘nav_menu_item’)
  • Attachment (‘attachment’)
  • Template parts (‘wp_template_part’)
  • Block templates (‘wp_template’)

Custom Post Types are post types that you create by yourself. These are helpful for material that doesn’t fit ready-made post/page formats.

Before you read further.... Free Download (PDF)

Secret Side Door

Secret Google Search Tactic That Will Skyrocket Your Sales, Connect You to the Perfect Partners, Influencers & Affiliates and Send Your Google Rankings Soaring! FREE when you sign up for Digital Creators Edge, a free newsletter for Digital Creators who wish to take their business to the next level.

For instance, if you plan to run a library website, setting up a book genre post type is a must. You can also generate other custom post types for authors, publishing dates, book volume, and so on.

Many plugins assist you to create custom post types in WordPress easily with a few clicks. The most popular WordPress custom post type plugins are WooCommerce and WPForms.

Why You Need a WordPress Custom Post Type

You should carefully consider your demand before diving into the creation of a new post type. There are many situations when a regular page or post can meet all your needs.

The following are some indications that custom post types should be called:

  • Some of the content you are about to upload is not posts such as a shop page.
  • You need extra fields to insert more data besides your content.
  • The content you want to upload requires a fresh display, not just pages or posts.

You can refer to our article on when to use WordPress custom post types for more information.

How to Display Custom Post Type in WordPress

Before we start, make sure you have created the custom post types you intend to show on your website. You can create custom post types WordPress without plugin if you are familiar with coding. If that’s not the case, some plugins like CPT UI can give you a hand.

pda-custom-post-type-ui-plugin

#1 Default Archive Template

To begin, create a new menu item by going to Appearance on the menu bar and choose Menus. You may access your new post type using the special URL below.

If you’re using SEO (search engine optimization) friendly permalinks, the URL for the custom post type may look like this: “http://example.com/books”. In contrast, your URL will look like: “http://example.com/?post_type=books”.

Make sure to change “books” and “example.com” to reflect your post type names and domain, respectively.

pda-display-custom-post-type-wordpress-archive-template

When all the steps above are done, remember to click the Save Menu button and go to the front end. Your custom post type is available on your website.

#2 Custom Templates

If the appearance that archive page brings to your custom post type does not satisfy you, custom templates will meet your needs.

  1. In the theme directory, create an archive-books.php file. Don’t forget to change the “books” to your own post type.
  2. Duplicate and move the theme-relating contents from archive.php to archive-books.php template. From there, you can make adjustments to get things set up the way you want. The newly-set-up template will display whenever users access your custom post type’s archive page.

For your single entry, the steps are quite similar. You have to create single-books.php in the theme directory. Always remember to change “books” into your custom post type. Copy all the theme templates from single.php to single-books.php and you can start customizing.

#3 Front Page

By utilizing the custom post type, you can keep your unique material distinct from standard posts. Still, there are other ways to present your custom post type and the front page is one of your choices.

To do that, you have to insert code into functions.php file. However, it is risky because this modification can damage the whole site. We recommend using WPCode to add your custom code instead.

  1. After installing and activating, in the Code Snippets, click Add Snippet. Tap on Use snippet in the Add Your Custom Code section.

pda-add-custom-code-wpcode

2. You will be transferred to the Create Custom Snippet site. Copy the code below paste it into the Code Preview box:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );

function add_my_post_types_to_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'books' ) );
   return $query;
}

3. Turn on the Active switch and hit the Save Snippet button. Remember to change “books” into your custom post type.

pda-create-custom-snippet-wpcode

#4 WP_Query

If you’re comfortable with code, WordPress Loop can help you display your custom post type. To make use of a custom post type, just paste the following code into your template.

<?php
$args = array( 'post_type' => 'books', '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; ?>

pda-wp-query

This code determines the amount of posts in a page and post types in the parameters for newly-created WP_Query class. Within the loop, it processes the query, obtains the posts, and shows them to the users.

Make Full Use of WordPress Custom Post Type

Congratulations! You’ve discovered 4 simple ways to display custom post types in WordPress.

If coding is your strength, just display your modified post types on Front Page or using WP_Query. If not, tinkering with the template might be a better option. We hope that you have found the most suitable methods.

In case you need to hide WordPress custom post types, the Protect WordPress Pages & Posts plugin could help you do that. Check out our guide for more details.

Don’t forget to subscribe to our website for more helpful information.