As a programmer, I always make my codes clean and efficient as much as possible.

WordPress in the other hand provides functions with lots of arguments. The one I noticed is the register_post_type function. Creating multiple post type need to repeatedly use the function.

Let’s say, I wanted to create 5 custom post types. Oh my! I can’t imagine how long my code will be.

In this article, I will show you how to efficiently create a custom post type without repeating the register_post_type function.

I created a function below that will only accept the arguments we actually need. Paste the function into your theme’s functions.php.

if( !function_exists( 'ywt_create_post_type' ) ) {
    function ywt_create_post_type(
        $post_type,
        $taxonomy,
        $label,
        $supports = false,
        $menu_icon = 'dashicons-admin-post',
        $is_public = true,
        $pt = true,
        $tx = true,
        $txname = null,
        $notsearchable = false,
        $rewrite = false
    ){
        //define default values
        $public = true;
        $queryable = true;
        $query_var = true;

        if ($rewrite == false) {
            $pt_slug = $post_type;
            $tx_slug = $taxonomy;
        } else {
            $pt_slug = 'basename/%taxonomy_name%';
            $tx_slug = 'basename';
        }
        if (!$menu_icon) {
            $menu_icon = 'dashicons-admin-post';
        }
        if (true == $is_public) {
            $public = true;
            $queryable = true;
            $query_var = true;
        } elseif (false == $is_public || !$is_public) {
            $public = true;
            $queryable = false;
            $query_var = false;
        }
        if ($pt === true) {
            register_post_type(
                $post_type,
                array(
                    'labels' => array(
                        'name' => _x($label, 'post type general name'),
                        'singular_name' => _x($label, 'post type singular name'),
                        'menu_name' => _x($label . 's', 'admin menu'),
                        'name_admin_bar' => _x($label, 'add new on admin bar'),
                        'add_new' => _x('Add New', 'question'),
                        'add_new_item' => __('Add New ' . $label),
                        'new_item' => __('New ' . $label),
                        'edit_item' => __('Edit ' . $label),
                        'view_item' => __('View ' . $label),
                        'all_items' => __('All ' . $label . 's'),
                        'search_items' => __('Search ' . $label),
                        'parent_item_colon' => __('Parent ' . $label . ' : '),
                        'not_found' => __('No ' . $label . ' found.'),
                        'not_found_in_trash' => __('No ' . $label . ' found in Trash.'),
                    ),
                    'public' => $public,
                    'exclude_from_search' => $notsearchable,
                    'publicly_queryable' => $queryable,
                    'query_var' => $query_var,
                    'rewrite' => array('slug' => $pt_slug),
                    'has_archive' => false,
                    'menu_position' => null,
                    'menu_icon' => $menu_icon,
                    'supports' => $supports,
                    'taxonomies' => array($taxonomy)
                )
            );
        }
        if ($tx === true) {
            $txnamemulti = 'Categories';
            if ($txname == null) :
                $txname = 'Category';
            else :
                $txnamemulti = $txname . 's';
            endif;
            register_taxonomy(
                $taxonomy,
                $post_type,
                array(
                    'hierarchical' => true,
                    'labels' => array(
                        'name' => _x($label . ' ' . $txname, 'taxonomy general name'),
                        'singular_name' => _x($label . ' ' . $txname, 'taxonomy singular name'),
                        'search_items' => __('Search ' . $txname),
                        'all_items' => __('All ' . $txnamemulti),
                        'parent_item' => __('Parent ' . $txname),
                        'parent_item_colon' => __('Parent ' . $txname . ':'),
                        'edit_item' => __('Edit ' . $txname),
                        'update_item' => __('Update ' . $txname),
                        'add_new_item' => __('Add New ' . $txname),
                        'new_item_name' => __('New ' . $txname . ' Name'),
                        'menu_name' => __($txname),
                    ),
                    'show_ui' => true,
                    'show_admin_column' => true,
                    'query_var' => $query_var,
                    'rewrite' => array('slug' => $tx_slug),
                )
            );
        }
    }
}

Parameters

$post_type = (string) post type
$taxonomy = (string) taxonomy. Taxonomy will be registered if $tx is set to true and
$label = (string) Single name of post type that will appear in menu
$supports = (array) post type supports ex. editor, thumbnail. Refer to codex
$menu_icon = (string) url or dashicons class of the icon to display in the menu. Default to ‘dashicons-admin-post’. Refer to codex
$is_public = (boolean) define if post type is publicly queryable
$pt = (boolean) Create the post type. True will create the post type. Default to true
$tx = (boolean) Create the taxonomy. True will create the taxonomy. Default to true
$txname = (string) the singular name of taxonomy to create
$notsearchable = (boolean) Define if the post type is not searchable. Default to false
$rewrite = (boolean) Define if rewrite rules will be enabled. Default to false

Sample Usage

In this example, lets create two custom post types, portfolio and testimonial. The portfolio post type is publicly visible and query-able. The testimonial post type is private and not accessible in single template.

function ywt_cpt_init() {
	//create portfolio post type
	ywt_create_post_type( 'portfolio', '', 'Portfolio', array('title', 'editor', 'thumbnail'), 'dashicons-portfolio',  true, true, false );

	//create testimonial post type
	ywt_create_post_type( 'testimonial', '', 'Testimonial', array('title', 'editor', 'thumbnail'), 'dashicons-testimonial',  false, true, false );

}
add_action( 'init', 'ywt_cpt_init' );