How to take your WordPress sites to the next level with custom post types
Why use custom post types?
In order to create a movie website, we need to set up a database. To do so without custom post types would be extremely tricky, and potentially clash with our existing WordPress installation. However, our custom post type will have its own admin menu and custom editing page, if we wanted to we could even add custom taxonomies to the page with the names and properties that suit the project. Custom post types are what take WordPress from a blogging platform to a full-blown CMS. They give us the freedom to set up our movie website without any nasty hacks.Creating our movie post type
In this article I’ll lay out all the code that is necessary to create a custom post type, and then we’ll go through it line-by-line so you can learn what each part does and customize it to your needs. Here’s the full code that gets added to your functions.php file:add_action( 'init', 'register_movie' ); function register_movie() { $labels = array( 'name' => 'Movies', 'singular_name' => 'Movie', 'add_new' => 'Add New', 'add_new_item' => 'Add New Movie', 'edit_item' => 'Edit Movie', 'new_item' => 'New Movie', 'view_item' => 'View Movie', 'search_items' => 'Search Movies', 'not_found' => 'No movies found', 'not_found_in_trash' => 'No movies found in Trash', 'menu_name' => 'Movies', ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'description' => 'Here you will add all the movies for the database', 'supports' => array( 'title', 'editor', 'thumbnail' ), 'taxonomies' => array( 'genre', 'movies', 'year' ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, //'menu_icon' => the image link here, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => true, 'capability_type' => 'post' ); register_post_type( 'movie', $args ); }As you can see a somewhat big chunk of code goes into creating a custom post type but if you understand it you will be able to get this code and adapt it to your projects. In the first line we hook the function with our custom post type to the init and this means our function will fire when WordPress does so that we will always have it in our dashboard:
add_action( 'init', 'register_movie' );
The labels
In the next line we start by declaring the name of our function and a variable with all the labels that will be associated with our movie post type so that everything can be customized. The first thing we declare in the labels is the name of our custom post type, in plural and singular form:'name' => 'Movies', 'singular_name' => 'Movie',In the next two lines we have to specify our Add New text (if we wish to change it) and then we set Add New Movie so that when we are adding a new movie we have a custom experience instead of adding a movie and having a headline saying 'Add New Post'.
'add_new' => 'Add New', 'add_new_item' => 'Add New Movie',After the labels for creating a new movie, we need to set the labels for editing, the new item text (by default is New Post/New Page) and we also need to set the view post text:
'edit_item' => 'Edit Movie', 'new_item' => 'New Movie', 'view_item' => 'View Movie',Now in the labels we move on to the search capabilities in the wordpress admin and our labels for that. We need to set labels for when we search movies, when no results are found, and also when no results are found in the trash:
'search_items' => 'Search Movies', 'not_found' => 'No movies found', 'not_found_in_trash' => 'No movies found in Trash',The last label speaks for itself, in here we have to place the name we want the custom post type to have in the menu UI, in this case we’re just sticking with “Movies”:
'menu_name' => 'Movies', );
The arguments
Now we have to move to our arguments, for that I created another variable that will hold all the arguments, I called it args. The first argument it asks for is the labels and all we have to do is point to the labels variable we just examined, like so:$args = array( 'labels' => $labels,In the next line we need to set whether our post type will be hierarchical like pages or not (like posts). In my case I don't want movies to be hierarchical so I’ve set it to false. The next line is just an optional description of the post type.
'hierarchical' => false, 'description' => 'Here you will add all the movies for the database',The next line is an important one; in here we have to specify what our custom post type will support, what fields it will have. The options for this field are:
- 'title'
- 'editor'
- 'author'
- 'thumbnail'
- 'excerpt'
- 'trackbacks'
- 'custom-fields'
- 'comments'
- 'revisions'
- 'page-attributes'
- 'post-formats'
'supports' => array( 'title', 'editor', 'thumbnail','comments' ),In the next line we need to specify what taxonomies it will be using, and since we will be creating custom taxonomies those are the ones that will be added in this line:
'taxonomies' => array( 'genre', 'actors', 'year' ),The next three lines have to do with the post type's visibility in the administration area and all I do is set all of that to true:
'public' => true, 'show_ui' => true, 'show_in_menu' => true,Next we move to the position of the menu in which the post type should appear. Here we also have a lot options to choose from:
- 5 - Below Posts
- 10 - Below Media
- 15 - Below Links
- 20 - Below Pages
- 25 - Below comments
- 60 - Below first separator
- 65 - Below Plugins
- 70 - Below Users
- 75 - Below Tools
- 80 - Below Settings
- 100 - Below second separator
'menu_position' => 5,In the next line we take care of the icon, we can set our own icon or leave it blank and we will get the posts icon instead, the line after that takes care of whatever we want this post type to appear for selection in our menus.
'menu_icon' => //the image link here, 'show_in_nav_menus' => true,In the next 3 lines we add the post type's capabilities; we first set whether we want that post type to be queried on the front end, then we decide if we want the post type results to be excluded from searches and finally we decide if we want an archive for the movies post type:
'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true,In the next line we set the query variable for our post type and this will define how the URL will look, for this option we have three possible parameters: we can set it to true and then we can reach the movie by using /?movie=name_of_movie; we can set it to a string so that movie in the URL will be replaced by anything we want, such as “show” and we would have to use /?show=name_of_movie in order to reach the same movie; the last option is to set it to false and this way you make it impossible to reach a movie using the query_var. In my case, and with the latter option in mind, I set my query var to true so that we can reach it with the query_var of movie:
'query_var' => true,In the next line we decide if we want the movies to be exportable and then we choose the slug for this post type, in my case I just stuck with true to have 'movie' as the slug but you can choose any string to be the slug and you actually have plenty of options, this parameter is an extensive one.
'can_export' => true, 'rewrite' => true,The final line of our arguments is where we set the capability type of our post and since I want it to have the exact same ones as normal posts, I just gave it the value of post, like so:
'capability_type' => 'post' );Our labels and arguments are done , all we need to do now is register our post type and the register_post_type function takes two parameters, the first one is the name of our custom post type (this has a maximum of 20 letters and with no capital letters or spaces) and the second one is the arguments for the post type and in this one we will just place our args variable:
register_post_type( 'movie', $args ); }Our post type is created and completely functional and all you need to have it show up in your pages is some wp_query magic.
Final words
I hope you can see why so many people use custom post types in WordPress. This article was meant to give you an understanding of the process of creating a custom post type, and give you a starting point for creating your own amazing custom post types. Featured image/thumbnail, customized image by ATOMIC Hot Links, via Flickr.Read Next
10+ Best Resources & Tools for Web Designers (2024 update)
Is searching for the best web design tools to suit your needs akin to having a recurring bad dream? Does each…
By WDD Staff
3 Essential Design Trends, April 2024
Ready to jump into some amazing new design ideas for Spring? Our roundup has everything from UX to color trends…
How to Plan Your First Successful Website
Planning a new website can be exciting and — if you’re anything like me — a little daunting. Whether you’re an…
By Simon Sterne
15 Best New Fonts, March 2024
Welcome to March’s edition of our roundup of the best new fonts for designers. This month’s compilation includes…
By Ben Moss
LimeWire Developer APIs Herald a New Era of AI Integration
Generative AI is a fascinating technology. Far from the design killer some people feared, it is an empowering and…
By WDD Staff
20 Best New Websites, March 2024
Welcome to our pick of sites for March. This month’s collection tends towards the simple and clean, which goes to show…
Exciting New Tools for Designers, March 2024
The fast-paced world of design never stops turning, and staying ahead of the curve is essential for creatives. As…
Web Tech Trends to Watch in 2024 and Beyond
It hardly seems possible given the radical transformations we’ve seen over the last few decades, but the web design…
By Louise North
6 Best AI Productivity Apps in 2024
There’s no escaping it: if you want to be successful, you need to be productive. The more you work, the more you…
By Simon Sterne
3 Essential Design Trends, February 2024
From atypical typefaces to neutral colors to unusual user patterns, there are plenty of new website design trends to…
Surviving the Leap from College to Real-World Design
So, you’ve finished college and are ready to showcase your design skills to the world. This is a pivotal moment that…
By Simon Sterne
20 Mind-Bending Illusions That Will Make You Question Reality
Mind-bending videos. Divisive Images. Eye-straining visuals. This list of optical illusions has it all. Join us as we…
By Max Walton