How to add custom post type to wordpress?

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:

$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',
'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

$args1 = array(
'posts_per_page' => '-1',
'post_type' => 'discourses', /* This is where you should put your Post Type */
'orderby'=> 'menu_order',
'tax_query' => 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;

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.

Please follow and like us:


Basic Coding Standards


How to write angular services in angular 4 ?


  1. Fabulous, what a blog it is! This webpage provides helpful data to us,
    keep it up.

  2. I got this web page from my buddy who shared with me on the topic of this web page and now this time
    I am browsing this website and reading very informative posts at this time.

  3. Pretty! This was an incredibly wonderful post. Many thanks for providing this info.

  4. I often visit your blog and have noticed that you
    don’t update it often. More frequent updates will give your page higher
    rank & authority in google. I know that writing articles takes a lot of time,
    but you can always help yourself with miftolo’s tools which will shorten the time of creating an article to a couple of seconds.

    • Harsh

      Working on couple of new articles. Shall update them soon.

  5. minecraft

    Greetings! I’ve been following your website for a while now and finally
    got the bravery to go ahead and give you a shout out from Huffman Texas!
    Just wanted to tell you keep up the fantastic work!

  6. minecraft

    Thanks for one’s marvelous posting! I genuinely enjoyed reading it, you happen to be
    a great author.I will always bookmark your
    blog and will often come back in the future.
    I want to encourage you to ultimately continue your
    great posts, have a nice morning!

  7. minecraft

    It’s enormous that you are getting ideas from this article as well
    as from our discussion made here.

  8. minecraft

    Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and visual appeal.
    I must say you have done a awesome job with this.
    Also, the blog loads very quick for me on Internet explorer.
    Exceptional Blog!

  9. minecraft

    Very descriptive post, I loved that a lot. Will there be a part 2?

  10. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! However,
    how can we communicate?

  11. Good post! We will be linking to this great content on our website.
    Keep up the good writing.

  12. Greetings! Very helpful advice in this particular article!
    It’s the little changes that will make the greatest changes.
    Thanks for sharing!

  13. I’m impressed, I must say. Seldom do I encounter a blog
    that’s both educative and entertaining, and let me tell you, you have hit
    the nail on the head. The problem is an issue that too few men and women are speaking intelligently about.
    I am very happy I came across this during my search for something concerning this.

  14. I am really impressed with your writing skills and
    also with the layout on your weblog. Is this a paid theme or
    did you customize it yourself? Anyway keep up
    the excellent quality writing, it’s rare to see a nice blog like this one
    these days.

  15. Hi there! This blog post could not be written much better!
    Reading through this post reminds me of my previous roommate!

    He continually kept preaching about this. I
    will forward this post to him. Fairly certain he’ll have a very good read.

    Thanks for sharing!

  16. I really like looking through an article that will make people think.
    Also, thanks for allowing me to comment!

  17. I know this if off topic but I’m looking into starting my own blog and was wondering what
    all is required to get setup? I’m assuming having
    a blog like yours would cost a pretty penny? I’m not very web smart
    so I’m not 100% certain. Any suggestions or advice would be greatly appreciated.
    Many thanks

Comments are closed.

Powered by WordPress & Theme by Anders Norén

Help us by spreading the word. :)

Follow by Email