• Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

Easy Web Design Tutorials

WordPress Tutorials and more

  • Home
  • WordPress
    • How To
    • Plugins
    • Themes
      • Genesis
      • Themify
      • Mysite myway
    • Security
    • Developer
    • Teaching
    • Conference
    • Chat
  • Blog
  • About
  • Freelance
    • Plugins
    • Teaching
    • Need help?
    • CV og ref.
  • Contact

15 August - 2018 By Paal Joachim 1 Comment
Last updated on: August 15, 2018

Creating a movie custom post type

I made the folder lib inside the root folder of the child theme. Then added a php file I named movie-custom-post.php with the following code:

<?php
/**
* Register Movie Type
*
* @author Ren Ventura <EngageWP.com> - with some adjustments by Paal Joachim
* @link http://www.engagewp.com/nested-loops-custom-wordpress-queries
*/

add_action( 'init', 'rv_movie_cpt' );
function rv_movie_cpt() {

   $labels = array(
       'name' => _x( 'Movie', 'post type general name', 'engwp' ),
       'singular_name' => _x( 'Movie', 'post type singular name', 'engwp' ),
       'menu_name' => _x( 'Movies', 'admin menu', 'engwp' ),
       'name_admin_bar' => _x( 'Movie', 'add new on admin bar', 'engwp' ),
       'add_new' => _x( 'Add New', 'Movie', 'engwp' ),
       'add_new_item' => __( 'Add New Movie', 'engwp' ),
       'new_item' => __( 'New Movie', 'engwp' ),
       'edit_item' => __( 'Edit Movie', 'engwp' ),
       'view_item' => __( 'View Movie', 'engwp' ),
       'all_items' => __( 'All Movies', 'engwp' ),
       'search_items' => __( 'Search Movies', 'engwp' ),
       'parent_item_colon' => __( 'Parent Movie:', 'engwp' ),
       'not_found' => __( 'No Movies found.', 'engwp' ),
       'not_found_in_trash' => __( 'No Movies found in Trash.', 'engwp' )
  );

  $args = array(
      'description' => __( 'Movie', 'engwp' ),
      'labels' => $labels,
      'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions' ), 
   // comments was removed to disable comments.
      'hierarchical' => false,
      'public' => true,
      'publicly_queryable' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => 'filmer' ), /* changed from movies to filmer */
      'show_ui' => true,
      'menu_icon' => 'dashicons-format-video',
      '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,
      'capability_type' => 'post',
 );

  register_post_type( 'movie', $args );

}

add_action( 'init', 'rv_create_movie_taxonomies' );
function rv_create_movie_taxonomies() {

   // Add new taxonomy, make it non-hierarchical (like tags)
   $labels = array(
      'name' => _x( 'Year Made', 'taxonomy general name' ),
      'singular_name' => _x( 'Year', 'taxonomy singular name' ),
      'search_items' => __( 'Search Years' ),
      'all_items' => __( 'All Years' ),
      'parent_item' => __( 'Parent Year' ),
      'parent_item_colon' => __( 'Parent Year:' ),
      'edit_item' => __( 'Edit Year' ),
      'update_item' => __( 'Update Year' ),
      'add_new_item' => __( 'Add New Year' ),
      'new_item_name' => __( 'New Year Name' ),
      'separate_items_with_commas' => __( 'Separate Years with commas' ),
      'add_or_remove_items' => __( 'Add or remove Years' ),
      'choose_from_most_used' => __( 'Choose from the most used Years' ),
      'not_found' => __( 'No Years found.' ),
      'menu_name' => __( 'Year Made' ),
  );

  $args = array(
      'hierarchical' => false,
      'labels' => $labels,
      'show_ui' => true,
      'show_admin_column' => true,
      'update_count_callback' => '_update_post_term_count',
   // 'query_var' => true,
   // 'show_in_nav_menus' => false,
      'public' => true,
      'publicly_queryable' => true,
      'has_archive' => true,
  );

  $years = array( 'rewrite' => array( 'slug' => 'movie-year' ) );
  $movie_args = array_merge( $args, $years );

  register_taxonomy( 'movie_years', 'movie', $movie_args );

}


//* Create Movie Type custom taxonomy (category)
add_action( 'init', 'custom_type_taxonomy' );
function custom_type_taxonomy() {

   register_taxonomy( 'movie-type', 'movie',
     array(
        'labels' => array(
        'name' => _x( 'Movie Category', 'taxonomy general name', 'text_domain' ),
        'add_new_item' => __( 'Add New Movie Category', 'text_domain' ),
        'new_item_name' => __( 'New Movie Type', 'text_domain' ),
     ),
       'exclude_from_search' => true,
       'has_archive' => true,
       'hierarchical' => true,
       'rewrite' => array( 'slug' => 'movie-type', 'with_front' => false ),
       'show_ui' => true,
       'show_tagcloud' => false,
   )
);

}

In the functions.php file I added:

require_once( get_stylesheet_directory() . '/lib/movie-custom-post.php' );

 

NB! After having created a custom post type one needs to go to Settings -> Permalinks and refresh them. Bascially just clicking the Save Changes should work.

The result:

Movie-custom-post

 

Go to part 2 as I show how to adjust the backend post list and a custom post type columns.

 


 

It is a lot easier to just use an online generator or a plugin. Such as..
wordpress.org/plugins/custom-post-type-ui
wordpress.org/plugins/types

codeinwp.com/blog/top-custom-post-types-plugins-for-wordpress/

 

Resources:
Based on Rens excellent tutorial
https://premium.wpmudev.org/blog/creating-content-custom-post-types/
https://generatewp.com/post-type/
https://www.idiotinside.com/2015/02/18/how-to-disable-comments-on-custom-post-type/

 

Share this:

  • Email

Categories: Developer, Featured, WordPress

Paal Joachim Romdahl

I enjoy teaching and creating tutorials. As well creating web sites.
I help people gain WordPress knowledge through my easy to follow tutorials and specialized training. Contact me for more information on how I can improve your WordPress skills and to help get your web site quickly up and running.

Comments

  1. alisan says

    9 December - 2018

    great article…

    Reply

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2021 · By Easy Web Design Tutorials · Built on the Genesis Framework · WordPress · Log in · ⇪

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.