How to Create Custom Post Type in WordPress

Custom post types are content types like posts and pages. Since WordPress evolved from a simple blogging platform into a robust CMS, the term post stuck to it. However, a post type can be any kind of content.

You can create your own custom post types and call them whatever you want. For example if you run a notice panel in your website, then you would probably want to create a notice post type. This post type can have different custom fields and even its own custom category structure, file upload option OR file attached field.

You just copy below code & paste in to your theme function.php file

Custom Post Type Code

// NOTICE POST TYPE

add_action(‘init’, ‘notices_register’);
function notices_register() {
$labels = array(
‘name’ => __(‘Notices’, ‘post type general name’),
‘singular_name’ => __(‘New Notices’, ‘post type singular name’),
‘add_new’ => __(‘Add Notices’, ‘notices item’),
‘add_new_item’ => __(‘Add Notices’),
‘edit_item’ => __(‘Edit Notices’),
‘new_item’ => __(‘New Notices’),
‘view_item’ => __(‘View Notices’),
‘search_items’ => __(‘Search Notices’),
‘not_found’ => __(‘Nothing found’),
‘not_found_in_trash’ => __(‘Nothing found in Trash’),
‘parent_item’ => __( ‘Parent Notices’ ),
‘parent_item_colon’ => __( ‘Parent Notices:’ )
);

$args = array(
‘labels’ => $labels,
‘public’ => true,
‘show_ui’ => true,
‘capability_type’ => ‘post’,
‘hierarchical’ => false,
‘has_archive’ => false,
‘rewrite’ => true, //array(‘slug’ => ‘testimonials’, ‘with_front’ => false ),
‘query_var’ => true,
‘menu_icon’ => get_stylesheet_directory_uri() . ‘/images/notice.png’,
‘menu_position’ => 4,
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’, ‘revisions’)
);

register_post_type( ‘notices’ , $args );
flush_rewrite_rules();
register_taxonomy( “notices-categories”,
array( “notices” ),
array( “hierarchical” => true,
“show_ui” => true,
“show_tagcloud” => false,
“hasArchive” => true,

“labels” => array(‘name’=>”Notices Categories”,’add_new_item’=>”Add New Category”),
“singular_label” => __( “Category” ),
“rewrite” => array( ‘slug’ => ‘notices-cat’,
// This controls the base slug that will display before each term
‘with_front’ => false)) );

add_action(“admin_init”, “notice_info”);
function notice_info(){
add_meta_box(‘notice_info_meta’, ‘Add Notice Information’, ‘notice_info_fnc’, ‘notices’, ‘normal’, ‘high’);
}
function notice_info_fnc(){
global $post;
if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) return $post_id;
$custom = get_post_custom($post->ID);
$ad_image = $custom[“ad_image”][0];
?>
<div class=”inside” style=”overflow:hidden;”>
<div style=”float:left; width:49%; margin-right:1%;”>
<p><strong>Upload Your File Here</strong><br />
<script>
jQuery(document).ready(function($){
var custom_uploader;
$(‘#upload_image_button’).click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title: ‘Choose File’,
button: {
text: ‘Choose File’
},
multiple: true
});
//When a file is selected, grab the URL and set it as the text field’s value
custom_uploader.on(‘select’, function() {
console.log(custom_uploader.state().get(‘selection’).toJSON());
attachment = custom_uploader.state().get(‘selection’).first().toJSON();
$(‘#upload_image’).val(attachment.url);
});
//Open the uploader dialog
custom_uploader.open();
});
});
</script>
<label for=”upload_image”>
<input id=”upload_image” type=”text” size=”36″ name=”ad_image” placeholder=”https//” value=”<?= $custom[“ad_image”][0] ?>” />
<input id=”upload_image_button” class=”button” type=”button” value=”Upload Doc. File” />
<br />Enter a URL or upload your file</label>
</div>
</div>
<?php }
}
// Save

add_action(‘save_post’, ‘save_notices’);
function save_notices(){
global $post;
if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE )
return;
if($post){
update_post_meta($post->ID, “pdf_file”, $_POST[“pdf_file”]);
update_post_meta($post->ID, “ad_image”, $_POST[“ad_image”]);
}
}
add_filter(“manage_edit-notices_columns”, “notices_edit_columns”);
function notices_edit_columns($columns){
$columns = array(
“cb” => “<input type=\”checkbox\” />”,
“title” => “Name / Title”,
“aboutnotices” => “Notices Short Details”,
“notices-cats” => “Category”,
“noticesthumb” => “Thumbnail”,
);
return $columns;
}
add_action(“manage_posts_custom_column”, “notices_custom_columns”);
function notices_custom_columns($column){
global $post;
switch ($column)
{
case “aboutnotices”:
the_excerpt();
break;
case “notices-cats”:
echo get_the_term_list($post->ID, ‘notices-categories’, ”, ‘, ‘,”);
break;
case “noticesthumb”:
if ( has_post_thumbnail() ) :
the_post_thumbnail(array (180, 120));
else :
echo ‘No featured image selected’;
endif;
break;
}
}

15 Responses to “How to Create Custom Post Type in WordPress”

  1. In the WordPress support forum, there are from time to time questions abut trackbacks. I haven’t used it myself, but I found a good tutorial on the OptiNiche cheap nfl jerseys china blog.

  2. Hi there, just became alert to your blog through Google, postcheap nike nfl jerseysand found that it’s really informative cheap nfl jerseys. I going to watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

  3. Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy to cheap nfl jerseys china. The overall look of your website is excellent, postcheap jerseyslet alone the content!

  4. Thanks for another fantastic post. Where else could anybody get that kind of info in such an ideal way of writing? I have a presentation next week, and I’m on the look for such information.

  5. Wow, wonderful blog layout! How long have you been blogging for? you made blogging look easy to cheap nfl jerseys china. The overall look of your site is excellent, postcheap nfl jerseys chinalet alone the content!

  6. We’re a group of volunteers and starting a new scheme in our community. Your web site offered us with valuable information to work on cheap nfl jerseys china. You’ve done a formidable job and our whole community will be grateful to you.

  7. hey there and thank you in your info ?I definitely picked up anything new from proper here postcheap nike nfl jerseys. I did alternatively experience a few technical issues the use of this website, as I skilled to reload the web site a lot of times previous to I may just get it to load properly. I had been thinking about if your web host is OK? Now not that I’m complaining, however sluggish loading circumstances instances will sometimes impact your placement in google and can harm your high quality ranking if ads and ****|advertising|advertising|advertising and *** with Adwords. postcheap jerseysAnyway I am adding this RSS to my e-mail and can look out for much more of your respective interesting content. Make sure you update this again very soon.

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

  9. I do agree with all the ideas you have presented in your postcheap jerseys. They’re really convincing and will certainly work. Still, the posts are too short for starters. Could you please extend them a little from next time? Thanks for the post.

  10. As a side note, the more we can use trackbacks, pingbacks, and backlinks to spread the love and our message, the better.we supply cheap nfl jerseys I encourage you to figure out how to use these tools as they can really make our words online much more effective and heard.

  11. I believe that is among the so much significant info for me. And i’m happy studying your article. But wanna statement on some normal things, The website taste is ideal,post the articles is actually nice D.Excellent job, cheers

  12. Hi there, just was alert to your blog thru Google, and found that it’s really informative. I am gonna be careful for brussels. I will appreciate if you proceed this in future. A lot of other folks will probably be benefited from your writing. Cheers!

  13. In the WordPress support forum, there are from time to time questions abut trackbacks. I haven’t used it myself, but I found a good tutorial on the OptiNiche cheap nfl jerseys china blog.

  14. Since I didn’t really understand how this works,we supply cheap nfl jerseys I did some searching and found an outstanding WordPress Trackback Tutorial that explains much of what I was finding confusing.

  15. Thanks for another fantastic article. Where else could anybody get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

​Leave a Comment

Comment authors age:




How to Create Custom Shortcode in WordPress

Create File Upload Option with jQuery in WordPress