Create Custom Post Types in WordPress

Custom Post Types and taxonomies are two powerful features of WordPress. When WordPress 2.9 was introduced it offered users the ability to add custom post types to their sites for the first time. WordPress 3.0 allows users to not only add custom post types but to also tie into the admin area of the WordPress backend all with fewer lines of code. By default WordPress comes with post and pages as the main content types. However you can create as many custom content types as you like, and these custom content types are referred to as Custom Post Types & also you can generate Custom Post Types.

register_post_type( $post_type, $args );

Register post type is the default post type register function.

Step 2
Register Post Type
Here’s the first bit of code we need to add to functions.php, which I’ll review below. There are two type custom post register system:

function codex_custom_init() {
$args = array(
'public' => true,
'label' => 'Books'
);
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );

2. Elaborate:

// Register Custom Post Type
function custom_product_type() {

$labels = array(
'name' => _x( 'Products', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Products', 'text_domain' ),
'parent_item_colon' => __( 'Parent Product:', 'text_domain' ),
'all_items' => __( 'All Products', 'text_domain' ),
'view_item' => __( 'View Product', 'text_domain' ),
'add_new_item' => __( 'Add New Product', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'edit_item' => __( 'Edit Product', 'text_domain' ),
'update_item' => __( 'Update Product', 'text_domain' ),
'search_items' => __( 'Search Product', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'product', 'text_domain' ),
'description' => __( 'Product Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'page-attributes', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'product', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_product_type', 0 );

Just replace your text-domain and post type. You can generate this code from Post Type Generator website. Anyone who’s worked with WordPress before will recognize the structure here. We’re adding an action when the WP Admin initializes to call the function function custom_product_type(). In that function we create two arrays, $labels and $args, and then use register_post_type to pull it all together.

Now, we need call the custom post by a template file. We we’ll create two files by named single-product.php & archive-product.php. Given bellow the code loop for those file.

// WP_Query arguments
$args = array (
'post_type' => 'product',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
);

// The Query
$portfolio_query = new WP_Query( $args );

// The Loop
if ( $portfolio_query->have_posts() ) {
while ( $portfolio_query->have_posts() ) {
$portfolio_query->the_post();
// do something
}
} else {
// no posts found
}

// Restore original Post Data
wp_reset_postdata();

New to site? Create an Account


Login

Lost Password?

Already have an account? Login


Signup