Home » How to create custom post type in wordpress programmatically

How to create custom post type in wordpress programmatically

  • by

Now day for blog site most popular CMS is that wordpress.com. There is very good Structure for content setup and make all seo friendly. Now if you go in there deeply there is Post, Media, Pages like that is by default create post type when you setup wordpress. If you want to make custom post type in wordpress you need to use plugin as well programmatically code.

How to create custom post type in wordpress programmatically

For create custom post type there is predefined function in wordpress.org  register_post_type() by that function you can be create custom post type in wordpress

Let we start how to make custom post type in wordpress

you should be follow below step for create custom post type.

Create a WordPress Custom Post Type Programmatically

For Example we create custom Post type of News

First you should open function.php of active theme (if i have avada theme active than open function.php of that theme)

 

/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'news',
// CPT Options
array(
'labels' => array(
'name' => __( 'news' ),
'singular_name' => __( 'News' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'news'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

After put this code save function.php after that you will be show it on admin side see below screenshot

Custom post type Programically

Explanation for custom post type in wordpress programmatically

For making custom post type you much be use add_action(‘init’) by that you need to post argument of that with register_post_type().

 

/*Custom Post type start xeeshop.com*/
function acw_post_type_newsa() {
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
$labels = array(
'name' => _x('news', 'plural'),
'singular_name' => _x('news', 'singular'),
'menu_name' => _x('news', 'admin menu'),
'name_admin_bar' => _x('news', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New news'),
'new_item' => __('New news'),
'edit_item' => __('Edit news'),
'view_item' => __('View news'),
'all_items' => __('All news'),
'search_items' => __('Search news'),
'not_found' => __('No news found.'),
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'has_archive' => true,
'hierarchical' => false,
);
register_post_type('news', $args);
}
add_action('init', 'acw_post_type_newsa');
/*Custom Post type end xeeshop.com*/

 

$supports: Indicates to the post type is compatible and supports all essential features.

$labels: Indicates to that the post type is referred properly to the admin area.

$args: Indicates to a permalink slug of the news, and a menu position located just beneath the Posts menu.

 

How to get custom post type data programmatically

Once, you have developed the code, your next task will be to create a new file called template-news.php and place it in your theme folder. As soon as you have created this file, add the following code to it.

/*Template Name: News*/
get_header();
query_posts(array(
   'post_type' => 'news'
));
while (have_posts()) : the_post(); ?>

 

 

After that create file you need to create and select template so you can be show good result as below screenshot
How to Create WordPress Custom Post Types

Leave a Reply

Your email address will not be published.