How to Create Custom Post Types In WordPress Without Plugin

How to Create Custom Post Types In WordPress Without Plugin

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

Today we are talking about custom post, 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 register_post_type() function. Custom Post Types one of the most powerful feature 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 offers website where you would like to display different type 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 URL structure of your custom posts type for example you are creating custom post type for offers but you want to URL should be “cashback-offers” then add below line of code in $args array.

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

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

Now go to WordPress Admin Dashboard, Click on Offers menu visible in left side and try to create a custom post type. The url structure for custom post should be as below:

http://www.yourdomain.com/offers/your-post-url/

Step 2. How to create custom post categories.

Well we can also create custom categories for custom post type. Add the following function to create the custom post categories. Copy below code and past into functions.php file inside your theme folder or can can create 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.

≷h1>How to show Recent Offers≷/h1>
≷?php 
$post_args = array( 'post_type' => 'offers', 'posts_per_page' => 10 );
$post_query = new WP_Query( $post_args ); 
 
if ( $post_query->have_posts() ) :
	while ( $post_query->have_posts() ) : $post_query->the_post();
	?>
	≷h2>≷?php the_title(); ?>≷/h2>
	≷div class="content">
		≷?php the_content(); ?>
	≷/div>
	≷?php wp_reset_postdata(); ?>
	≷?php endwhile; // ending while loop ?>
	≷?php else:  ?>
	≷p>≷?php _e( 'Sorry, no offers found.' ); ?>≷/p>
≷?php endif; // ending condition ?>

Step 4. How to create custom archive page for custom posts

By default custom posts use the same archive page as your simple posts. But if you would like to create new page for showing custom posts according to categories then it is pretty simple to do. Follow the simple below steps.

  1. Go to your theme folder and create duplicate of 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 archive. Remember above we created “offers” as taxonomy for the custom posts categories for offers. You may replace “offers” with your own custom taxonomy.
  2. Now all your custom posts with taxonomy “offers” will show in archive-offers.php template. If you want to change layout of page or any other code inside archives-offers.php will effect only for custom posts archive page. You can use below code.
≷?php
/*
 * The template for displaying all offers posts
*/
get_header(); ?>
≷div class="clearfix">≷/div>
≷div class="offer-layout">
	≷div class="container">
		≷?php
		if ( have_posts() ) :
			while ( have_posts() ) : the_post();
				echo '≷li>≷a href="'.get_the_permalink().'">'.get_the_title().'≷/a>≷/li>';
			endwhile;
		endif;
		?>
	≷/div>
≷/div>
≷?php
get_footer();
?>

The below URLs will use archive-offers.php template to display your all custom posts of offers.

http://yourdomain.com/offers

The below URLs will use archive-offers.php template to display custom posts for specific category.

http://yourdomain.com/offers/category

Step 5. How to create 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 archive page you can also create custom single page for the custom posts. It is pretty simple, just follow the below steps.

  1. Go to your theme folder and create duplicate of 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 the single.php file. Remember above we created “offers” as custom posts with name of “offers” so we renamed single.php to single-offers.php and you can replace “offers” according to your custom posts name.
  2. Now all your “offers” custle-offers.php template. If you want to change layout of page or any other code inside single-offers.php you can do according to your requirement and website design.
Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.