WordPress is a savior! Custom Post Type is cherry on cake.

We may or may not like it but, we all have to agree that its something that gives us as front-end developers freedom, to have their own CMS platform without any back-end support.

So, to add more to wordpress we often get the need on how to add a custom post type to our theme. In this post will explain each steps as to how to create and fetch too.

Step 1 – Open function.php

functions.php is a file where we can create our custom hooks, actions and also our custom post type. You’ll find it in the theme folder.

Step 2 – Paste the below code in functions.php

Here I am creating a custom post type for “Discourses” so here is the code:

//DISCOURSE POST TYPE CODE
$labels = array(
'name' => 'Discourses Type',
'singular_name' => 'Discourses',
'search_items' => 'Search Discourses Type',
'popular_items' => 'Popular Discourses Type',
'all_items' => 'All Discourses Type',
'parent_item' => 'Parent Discourses',
'edit_item' => 'Edit Discourses',
'update_item' => 'Update Discourses',
'add_new_item' => 'Add New Discourses',
'new_item_name' => 'New Discourses',
'separate_items_with_commas' => 'Separate Discourses Type with commas',
'add_or_remove_items' => 'Add or remove Discourses Type',
'choose_from_most_used' => 'Choose from most used Discourses Type'
);

$args = array(
'label' => 'Discourses Type',
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'discourses/discourses_type', 'with_front' => false ),
'query_var' => true,
);

register_taxonomy( 'discourses_type', 'discourses', $args );

register_post_type( 'discourses',
array(
'labels' => array(
'name' => __( 'Discourses' ),
'singular_name' => __( 'Discourse' )
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'rewrite' => array( 'slug' => 'discourses', 'with_front' => false ),
'has_archive' => true,
'supports' => array('title', 'page-attributes', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'comments')
)
);

Now, watch closely! In the code above for the custom post type “Discourses” replace it with your type exactly the same way. Its case sensitive and if you notice there is “s” at some places and at times not. The D is capital and at sometimes its not. So replace the keyword with full attention.

Step 3 – Fetch Data To Template.

Now, we have our custom post type of “Discourses” ready in our back-end. You can add posts, category and featured image too just like default posts of wordpress. So, now how do we fetch the data to a specific template/page? First, for custom post type you always need a category present only then you can fetch it. So, to fetch the data category is most important.

Wordpress Categories

Paste the below code in your template

<?php
$args1 = array(
'posts_per_page' => '-1',
'post_type' => 'discourses', /* This is where you should put your Post Type */
'orderby'=> 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'discourses_type',
'field' => 'slug',
'terms' => 'All' /* category you make name it here*/
)
)
);

$my_query1 = null;
$my_query1 = new WP_Query($args1);
if( $my_query1->have_posts() ) {
while ($my_query1->have_posts()) : $my_query1->the_post(); ?>

<!-- Your HTML comes here -->

<?php endwhile;
}
wp_reset_query();
?>

That’s it! Your custom post type is ready to go.
For any further help leave a comment or you can contact me on the details given on the sidebar.