How to Create Custom Post Type in WordPress Without Plugin?

WordPress means WOW it’s the world’s most popular CMS (Content Management System) which basically used for creating blogging websites but we can create any type of website with the help of WordPress.

How to Create Custom Post Type in WordPress Without Plugin?

Today we are talking about custom posts, All the post types are stored in the same database table (wp_posts) and differentiated by a column called post_type. By default, five post types are available in WordPress.

Post (Post Type: ‘post’)
Page (Post Type: ‘page’)
Attachment (Post Type: ‘attachment’)
Revision (Post Type: ‘revision’)
Navigation menu (Post Type: ‘nav_menu_item’)

We can create new post types which are called Custom Post Types using the register_post_type() function. Custom Post Types is one of the most powerful features of WordPress. WordPress custom post type feature helps you to divide up your website content in a more structured way.

In this article, we will show you step by step how to create custom post types in WordPress without using any third-party plugin. Also, you will know how to add taxonomy or category of custom post types in WordPress.

Suppose you have an offers website where you would like to display different types of offers and you want to separate the offer section from the posts section. In that case, you need a separate offers post type. This custom post type will have its own custom category and custom fields. You can also read how to upload file using ajax in WordPress

How to Create Custom Post Types Without Plugin
It’s very simple we will show you how to add Custom Post Types without using any third party plugin.

Step 1. How to create custom posts type function
Add custom post type function in functions.php file inside your theme folder. Copy below code and past into functions.php file inside your theme folder or can can create seprate file and include into functions.php file.

// Create section in dashboard of adding custom post.
function phpkida_offers_post() {
$labels = array(
'name' => _x( 'Offers', 'post type general name' ),
'singular_name' => _x( 'Offer', 'post type singular name' ),
'add_new' => _x( 'Add New', 'offers' ),
'add_new_item' => ( 'Add New Offers' ), 'edit_item' => ( 'Edit Offers' ),
'new_item' => ( 'New Offers' ), 'all_items' => ( 'All Offers' ),
'view_item' => ( 'View Offers' ), 'search_items' => ( 'Search Offers' ),
'not_found' => ( 'No act offers found' ), 'not_found_in_trash'=> ( 'No act offers found in the trash' ),
'parent_item_colon' => '',
'menu_name' => 'Offers'
$args = array(
'labels' => $labels,
'description' => 'Holds act offers specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
register_post_type( 'offers', $args );
add_action( 'init', 'phpkida_offers_post' );

How to change slug/url of custom posts type
If you want to change the URL structure of your custom posts type, for example, you are creating a custom post type for offers but you want to URL should be “cashback-offers” then add the below line of code in $args array.

'rewrite' => array( 'slug' => 'companies-act-procedures' ),

The initial segment of the above function defines the labels for the custom post type. You can change the label according to your post type. These labels will show up on the back-end of WordPress same as for pages and posts.

Now go to WordPress Admin Dashboard, click on the Offers menu visible on the left side and try to create a custom post type. The URL structure for the custom posts should be as below:
Step 2. How to create custom post categories.
Well, we can also create custom categories for the custom post types. Add the following function to create the custom post categories. Copy the below code and paste it into the functions.php file inside your theme folder or can create a separate file.

// Create Category for custom post
function phpkida_taxonomies_offers() {
$labels = array(
'name' => x( 'Offers Categories', 'taxonomy general name' ), 'singular_name' => _x( 'Offer Category', 'taxonomy singular name' ), 'search_items' => ( 'Search Offers Categories' ), 'all_items' => ( 'All Offers Categories' ), 'parent_item' => ( 'Parent Offers Category' ), 'parent_item_colon' => ( 'Parent Offers Category:' ), 'edit_item' => ( 'Edit Offers Category' ), 'update_item' => ( 'Update Offers Category' ), 'add_new_item' => ( 'Add New Offers Category' ), 'new_item_name' => ( 'New Offers Category' ), 'menu_name' => _( 'Categories' ),
$args = array(
'labels' => $labels,
'hierarchical' => true,
register_taxonomy( 'offers', 'offers', $args );
add_action( 'init', 'phpkida_taxonomies_offers', 0 );

Step 3. How to display most recent custom posts
Since we already created custom posts as well as categories for them. Now the question is how to show custom posts? So we can show custom posts like we are showing simple posts just needs to modify query code and show custom posts anywhere on the website.

If you are familiar with WordPress Query then below code is pretty easy to understand. Basically in below code we have query the custom posts. You may add below code anywhere in your index.php, footer.php, sidebar.php or page.php to display most recent custom posts.

<!-- wp:paragraph -->
<p>≷h1&gt;How to show Recent Offers≷/h1&gt;<br>≷?php<br>$post_args = array( 'post_type' =&gt; 'offers', 'posts_per_page' =&gt; 10 );<br>$post_query = new WP_Query( $post_args );</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>if ( $post_query-&gt;have_posts() ) :<br>while ( $post_query-&gt;have_posts() ) : $post_query-&gt;the_post();<br>?&gt;<br>≷h2&gt;≷?php the_title(); ?&gt;≷/h2&gt;<br>≷div class="content"&gt;<br>≷?php the_content(); ?&gt;<br>≷/div&gt;<br>≷?php wp_reset_postdata(); ?&gt;<br>≷?php endwhile; // ending while loop ?&gt;<br>≷?php else: ?&gt;<br>≷p&gt;≷?php _e( 'Sorry, no offers found.' ); ?&gt;≷/p&gt;<br>≷?php endif; // ending condition ?&gt;<br>Step 4. How to create a custom archive page for custom posts<br>By default custom posts use the same archive page as your simple posts. But if you would like to create a new page for showing custom posts according to categories then it is pretty simple to do. Follow the simple below steps.</p>
<!-- /wp:paragraph -->

Go to your theme folder and create a duplicate of the archive.php file to archive-offers.php, It’s very simple not to do so many changes we just need to add the custom posts taxonomy name along with the archive. Remember above we created “offers” as a taxonomy for the custom posts categories for offers. You may replace “offers” with your own custom taxonomy.
Now all your custom posts with taxonomy “offers” will show in an archive-offers.php template. If you want to change the layout of a page or any other code inside archives-offers.php will affect only for custom posts archive page. You can use the below code.

<!-- wp:paragraph -->
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>The template for displaying all offers posts<br>*/<br>get_header(); ?&gt;<br>≷div class="clearfix"&gt;≷/div&gt;<br>≷div class="offer-layout"&gt;<br>≷div class="container"&gt;<br>≷?php<br>if ( have_posts() ) :<br>while ( have_posts() ) : the_post();<br>echo '≷li&gt;≷a href="'.get_the_permalink().'"&gt;'.get_the_title().'≷/a&gt;≷/li&gt;';<br>endwhile;<br>endif;<br>?&gt;<br>≷/div&gt;<br>≷/div&gt;<br>≷?php<br>get_footer();<br>?&gt;<br>The below URLs will use archive-offers.php template to display your all custom posts of offers.</li></ul>
<!-- /wp:list -->
The below URLs will use the archive-offers.php template to display custom posts for a specific category.
Step 5. How to create a custom single page for custom posts
I think your next question will be how to show custom single posts so don’t worry similar to the archive page you can also create a custom single page for the custom posts. It is pretty simple, just follow the below steps.

Go to your theme folder and create a duplicate of the single.php file to single-offers.php, It’s very simple not to do so many changes we just need to add the custom post name along with the single.php file. Remember above we created “offers” as custom posts with the name of “offers” so we renamed single.php to single-offers.php and you can replace “offers” according to your custom posts name.
Now all your “offers” castle-offers.php template. If you want to change the layout of a page or any other code inside single-offers.php you can do it according to your requirement and website design.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button