How to Create Custom Post Types in WordPress

[easy-social-share counters=0 style="button"]

The best way to set up a custom post type in WordPress is actually by using a plugin. This technique is usually recommended for new simply because it is secure or very simple.
The first thing you can do is set up or activate the Custom Post Type UI plugin. Upon activation, the plugin will insert a new menu item in your WordPress admin menu known asCPT UI.
Right now go into CPT UI» Insert New at all to create a new custom post type.

cpt-ui

The Custom Post Type UI plugin at the same time make it easier to build custom taxonomies.
That is why the Insert new custom post type page is divided into 2 columns. On your left, you will have the form you need to have to fill up to set up your custom post type. On your correct, you will have a form to set up a custom taxonomy when you need only one.
In the custom post type column, 1st that you need to provide a name for your custom post type. This name are unable to better even more than twenty letters, e.g. movies,recipes, plan, glossary, and etc.

Next topic, you will need to provide you a label for your custom post type. This label will appear in your WordPress admin bar just as posts or pages. This also requires to beplural to be important. e.g. Movies, Tasty recipes, Deals, Glossary, and etc.

new-cpt
And after that you are required to provide a single form for your label. This single form may be used for WordPress to display guidelines or other user interface elements.
And finally type in a description for your custom post type. This description will be just useful to describe what your post type can do.
Right now you can click the ‘Create Custom Post Type’ key to insert the new custom post type. That’s all of.
You will also click the In advance Label Option or Advanced Options links to customize much more methods for your custom post type.

Creating a Custom Post Type Manually

The issue with the use of a plugin is the fact that your custom post types will probably try to escape in case the plugin is deactivated. Any kind of data you may have in those custom post types will always be there, but then your custom post type will be unregistered that will not be available from the admin area.
When you are working away at a client site or would not like to to install one more plugin, after that you can personally develop your custom post type by installing the neededcode in your theme’s functions.php file folder or even in a site-specific plugin ( Check: Custom Post Types Debate functions.php and Plugin ).
1st i will display a fast or easily work an example so that you will also learn how it is working. Check out this code:

// Our custom post type function

function create_posttype() {

                register_post_type( 'movies',

                // CPT Options

                                array(

                                                'labels' => array(

                                                                'name' => __( 'Movies' ),

                                                                'singular_name' => __( 'Movie' )

                                                ),

                                                'public' => true,

                                                'has_archive' => true,

                                                'rewrite' => array('slug' => 'movies'),

                                )

                );

}

// Hooking up our function to theme setup

add_action( 'init', 'create_posttype' );

Exactly what this code will do is always that it registers a post type ‘movies’ with an array of arguments. These types of arguments are the choices of our custom post type. This array provides 2 sections, the 1st section is labels, which in itself is an array. The 2nd section contains any other arguments just like public visibility, has archive, and slug that should be used for URLs with this post type.

Right now lets have a look at a detailed element of code that adds on a lot more option to your custom post type.

/*

* Creating a function to create our CPT

*/



function custom_post_type() {



// Set UI labels for Custom Post Type

                $labels = array(

                                'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),

                                'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),

                                'menu_name'           => __( 'Movies', 'twentythirteen' ),

                                'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),

                                'all_items'           => __( 'All Movies', 'twentythirteen' ),

                                'view_item'           => __( 'View Movie', 'twentythirteen' ),

                                'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),

                                'add_new'             => __( 'Add New', 'twentythirteen' ),

                                'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),

                                'update_item'         => __( 'Update Movie', 'twentythirteen' ),

                                'search_items'        => __( 'Search Movie', 'twentythirteen' ),

                                'not_found'           => __( 'Not Found', 'twentythirteen' ),

                                'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),

                );



// Set other options for Custom Post Type



                $args = array(

                                'label'               => __( 'movies', 'twentythirteen' ),

                                'description'         => __( 'Movie news and reviews', 'twentythirteen' ),

                                'labels'              => $labels,

                                // Features this CPT supports in Post Editor

                                'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

                                // You can associate this CPT with a taxonomy or custom taxonomy.

                                'taxonomies'          => array( 'genres' ),

                                /* A hierarchical CPT is like Pages and can have

                                * Parent and child items. A non-hierarchical CPT

                                * is like Posts.

                                */          

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

                );



                // Registering your Custom Post Type

                register_post_type( 'movies', $args );



}



/* Hook into the 'init' action so that the function

* Containing our post type registration is not

* unnecessarily executed.

*/



add_action( 'init', 'custom_post_type', 0 );

I hope you can see, I have set up a lot more option to the custom post type with his code. It will be able to add more features just like support for changes, featured image, custom fields, etc.
I have also connected this custom post type with a custom taxonomy called genres. And also notice that the section when we have now set up the hierarchical value to be fake. If you wish to your custom post type to respond just like Page, then you can definitely set up this value to right.
And also note continued use of twenty thirteen that is known as text domain. When your theme is translation ready therefore you would like your custom post types to be translated, then you definitely may need to refer to text domain useful for your theme. You will find your theme’s text domain inside style.css file folder in your theme directory site. Text domain would be discussed in the header of the file folder.

Displaying Custom Post Types on Your Site

WordPress is included with built-in support for showing your custom post types. When you have inserted just a few items into that new custom post type, the time is now to showthem on your web site.
You can find a number of techniques that you are able to using, each one of these have their positive aspects.

Displaying Custom Post Type Using Default Archive Template

1st you can easily simply go into Appearance » Menus or insert a custom url to your menu. This custom url is the url to your custom post type. If you use Search engine optimizing friendly permalinks and then your CPT’s URL will likely be something such as this:
http://example.com/movies 
When you are not using Search engine optimizing friendly permalinks, and then your custom post type URL can be something similar to this:
http://example.com/ ?post_type=movies 
Exchange for example.com with your personal domain name or movies with your custom post type name. Save your menu and then visit the front-end of your web site. You will find out the new menu you inserted, just in case you click it, this will show your custom post type archive page to use the archive.php template file folder in your theme.

Using Custom Templates for CPT Archives and Single Entries

When you don’t just like the appearance of the archive page for your custom post type, then you can certainly using dedicated template for custom post type archive. To do thisall you require to do is set up a brand new file in your theme directory and name it archive-movies.php. Exchange movies with the name of your custom post type.
For getting set up, you can actually copy the contents of your theme’s archive .php file into archive-movies.php template and then start adjusting it to meet your requirements. Right now at any time the archive page for your custom post type is viewed, this template can be used to show it.
In a similar way, also you can set up a custom template for your post type’s single entry show. To do this you are required to set up single-movies.php in your theme directory. Don’t forget to exchange movies with the name of your custom post type.
You can obtain set up by copying the contents of your theme’s single.php template into single-movies.php template then it start adjusting it to meet your requirements.

Displaying Custom Post Types on The Front Page

Only one benefit of to use custom post types is the fact that it will keep your custom content types apart from your daily posts. However, if you wish these to show among yourdaily post, then you can certainly do it by inserting this code into your theme’s functions.php file and a site-specific plugin:

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );



function add_my_post_types_to_query( $query ) {

                if ( is_home() && $query->is_main_query() )

                                $query->set( 'post_type', array( 'post', 'movies' ) );

                return $query;

}

Don’t forget about to exchange movies with your custom post type.

Querying Custom Post Types

In case you are informed about the coding and then would wish to run loop queries in your templates, and then this is how to do this (Similar: Which is a Loop? ) .
By querying the database, you can retrieve items from a custom post type.

<?php

$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );

$the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<h2><?php the_title(); ?></h2>

<div class="entry-content">

<?php the_content(); ?>

</div>

<?php wp_reset_postdata(); ?>

<?php else:  ?>

<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>

<?php endif; ?>

In this particular code, 1st in this article we have explained the post type or posts per page in the arguments for our new WP_Query class. And after that we ran our query, retrieved the posts or shown to them inside the loop.

Submit a comment

Pin It on Pinterest