Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a Custom Post Type in WordPress manually

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 247
    Comment on it

    Creating a Custom Post Type Manually

    You can create any custom post type in wordpress any name which you want like- If you are creating a movie review site then you can create a custom post type with the name 'movie'.

    Wordpress have some default post type which are -

    1. Post
    2. Page
    3. Attachment
    4. Revision
    5. Nav Menu

      If you use a plugin to create custom post type like movie, book etc.., the problem with this is if admin deactivate the plugin then any data you have in those custom post type will still be there, but your custom post type will be unregistered and will not be accessible from the admin area.

    So you can create a custom post type manually by adding the required code in your theme's function.php, first I am writing a full example to create a custom post type with the name 'movie'-

    // Function for the custom post type 'movie'

    function create_movie_PostType() {
    
            register_post_type( 'movies',
    
                    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_movie_PostType' );

    In this function I registers a post type 'movies' with an array of arguments. These arguments are the options of our custom post type. This array has two parts, the first part is labels, which itself is an array. The second part contains other arguments like public visibility, has archive, and slug that will be used in URLs for this post type.

    Now writing a detailed piece of code that adds more options in custom post type-

    /* Function to create our custom post type */

    function customPostType() {
    
            $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 Editorphp
    
                    '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 CPTphp
    
                    * is like Posts.
    
                    */      
    
                    'hierarchical' => false,
    
            // 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', 'customPostType', 0 );    
    
            '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', 'customPostType', 0 );

    Displaying Custom Post Types on The Front Page

    One advantage of using custom post types is that it keeps your custom content types away from your regular posts. However, if you would like them to display among your regular post, then you can do so by adding this code into your themes functions.php file or a site-specific plugin:

    add_action( 'pre_get_posts', 'add_my_movie_post_types_to_wp_query' );
    
    function add_my_movie_post_types_to_wp_query( $query ) {
    
            if ( is_home() && $query->is_main_query() )
    
                    $query->set( 'post_type', array( 'post', 'movies' ) );
    
            return $query;
    
    }

    Querying Custom Post Types

    If you are familiar with the coding and would like to run loop queries in your templates, then here is how to do that (with the the_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="cpt-content">  
    <?php the_content(); ?> 
    </div>    
    <?php wp_reset_postdata(); ?>
    <?php else:  ?>    
    <?php _e( 'Sorry, no posts in movies post type.' ); ?>    
    <?php endif; ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: