WordPress 101 - Part 18: How to create Custom Post Type - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys Alex Sierra thank you for checking this video and welcome to the 18th part of the series of tutorial WordPress 101 for beginner developers have you ever heard about Monster eyed monster ID is basically WordPress on steroids with this product you can build easily everything from a single blog page to a gigantic ecommerce to sell your product online if you don't want to waste time and you're looking for a product that it works out of the box and is really solid I suggest you to try and monstro it click on this video to learn more and access a 10% discount for Mon stride or whatever other theme you decide to purchase from the template monster repository welcome again in this lesson we're going to take a look at the custom post types so the custom post type is a really complex argument but actually really easy to use I mean let's take a look to understand better at the administration panel of WordPress usually when we want to create something new that is a page or a post or we have a new comments we have a new revision or we just simply upload a new media element there's an image or something else everything that we do everything we create from scratch is saved inside a WordPress database inside a my sequel database managed by WordPress all these informations every time we add something new is inputted inside one single table and the table is the WP post so just a question have you ever noticed in some premium theme or some custom theme that you download you have something else other than post media pages in common sometimes you have like portfolio or you have like reviews or slider you have some other sections where you can create and save new content inside WordPress those are custom post types so just to explore a little bit better if we access our my sequel database this is my local database based on my sequel if we access our WP - whatever name you decided for you double the base and we go into the WP underscore posts top table we're going to see here inside this table it saved all our content it's inside here so we have posts we have pages we have everything and if we go to the right and we check the title of our columns you will notice here we have a post type and inside a post type we have page revision sometimes we have a nav menu item we have a post and sometimes we also have attachment so these is the post type WordPress cannot create a custom table for every post type so he cannot create a table for a page a table for a post a table for an attachment everything has to be stored inside one place - to maintain the database as streamlined as possible easy to manage easy to update and easy to scale so WordPress gives us the ability to create another custom post type different from page revision post or whatever and WordPress accepts pretty much every type every word as a post die we can input whatever different type of content that we want in this way if we create a custom post type we can completely change the way we use WordPress in the administration panel so for example if we access the front-end and we'd say that I want to create another page called portfolio I can go inside pages here and add new let's create portfolio let's put the content this is my portfolio that's it or like K capital S P let's publish it let's go in the appearance menu the x-axis our current main navigation menu not footer let's add portfolio to our manual s period after the log save let's go batty an affront an refresh now we have the portfolio inside of our menu nice super simple super easy if we click on it now what I wanted you because we don't have a page and because we don't have something custom automatically WordPress is gonna call the index it's going to show our blog loop I don't want that I want in my portfolio to show a nice four columns grid of all my work it I want to create an actual portfolio so I don't want the sidebar I don't want the post navigation I want just a nice grid of whom my works I can do that in a couple of different ways the first easy way is to create a post and inside a post we can create a custom category called portfolio and then call that category every time we access the portfolio but that can create a problem if in our post page inside the administration panel we have all together blog post updates reviews and portfolio this page can be really hard to manage especially if different users are updating at the same time your WordPress installation or your theme that can be really confusing because everything is together is differ is difficult to manage to separate content and you have everything one page and is not really optimized so what I want to do I want to create a custom post type called portfolio to input just the portfolio easy right so to do that let's access our text editor and in the functions dot PHP that's where we're going to create our custom post type so let's copy the comment of section let's pretty here and it's called a section custom post type so to create a post type we have to write a lot of code because the post type gives us the custom post type gives the deputies you basically customize everything every single detail of the new post ID that we're creating so we have tons and tons of options and pretty much all the options are important we can avoid to specify some of those options because the default values is it's kind of okay so we're going to accept that but let's take a look at the most important arguments that you can specify in a custom post type so first of all let's create a function call function awesome custom post type and I don't need any arguments here let's open the function and let's start writing here so first we have to create two variables the first variable is the label variable and the second variable is the arcs variable in these two variables we're going to store an array we dole the attributes and information that WordPress needs to create a proper custom post type so let's start with the labels let's open our array of information go back closer with a semicolon and let me create some space here so everything is nicely aligned at the center first we have to create the name we have to decide the name of our custom post type so let's call this portfolio then the second parameter always put a comma after that the second parameter is the singular name the singular name is the name that is team that is going to be used inside your administration panel every time you access a single all post or single custom post type you're gonna be called single portfolio single whatever in my case portfolio is both can be used like both plural singular whatever so I can specify I can leave like portfolio like that or I can specify another custom name in this case I want to call it like just simply port folio I don't want to use a different name so now we have to specify the add new label so what's going to be the label used in the button to add a new portfolio item and in my case at folio item and the other parameters all items all these labels are referring to the administration labels all those links and menus names that you find inside the administration panels we have the ability to customize them here so all items let's remove portfolio items add new item and can be exactly IPPs can be exactly the same as add item then we have the label Edit item equal add it item it's pretty easy I raise David it's pretty boring new item label add sorry this is the new item here we can specify the beaut item label and everything is view item and here again specify the search item label if we want to search that inside our admin panel search portfolio news and here if we have a label not found in the administration panel we can use no items found and the other label is we do a research in the trash page in trash we can specify a custom no items found in trash , and here the last one finally parent item : is the parent item in the case in the remote case if we have an indentation like a portfolio item is underneath another portfolio atom but maybe not maybe yes I don't know whatever anyway we are I'm printing all the information that you can add customized inside these labels array a lot of these names you can avoid it if you don't specify the names WordPress is gonna fall back to the default name that is like a new post that new page or add new something else a new media attachment whatever so we can avoid to specify designer map if we want to customize everything the entire experience in the admin panel we have the ability to specify every single label to customize every single label the other parameter that is going to be also on array is the arguments parameter the argument is finally that amount of arguments of course that gives us the ability to customize entirely the structure of our custom post type by activating specific functions so let's open also here the array and as a first parameter we have to print inside all the labels that we define in our first variable so we have to create the arguments as a string labels and inside here we put the variable labels pretty easy right we have the other parameters the parameter public that is a boolean so can be through or false we want this custom post type to be publicly accessible so we have to specify it through as a value the other parameter is has archive so if we want our portfolio or our custom post type to have an archive page where when we click like the category or the tag of this portfolio it goes correctly to the archive page we have to specify through because also these has archived is a true parameter in a case of a media element so an attachment post type or a revision post type these types they don't need an archive so we can specify hazard kyv folds in our case we want our kyv so it's going to be true publicly underscore queryable it means that our custom post type can be publicly excessive with a custom query the other parameter goes together with the first one is the query bar so we are specifying the our custom post type has a query bar that can be accessed the query bar basically is just the name all lowercase that we're going to specify later while we'll register the post type the other parameter is the rewrite the gives us the ability to rewrite the custom slug so also this one is a boolean if we wanna hours lock to be like for example a name of your website comm slash portfolio it means that the rewrite has to be true because we want to create a custom slug based on the name of our custom post type we can specify the capability type capability underscore type is that value that basically grabs the default settings of a premade custom post type so in our case we want the same capability of a post another page as you notice like posts pages attachments revision they have different capabilities so when you when you create a new custom post type you can grab those default capabilities by from a pre-existing post type in our case we want the same capability of a post we can specify if this thing is as a hair key so is yeah kickers really e is really easy to make a typo so be careful probably I'm gonna have some error we don't want to be her key call because it's not a category is not something that is not like a page then we want to create some hierarchy inside this page structure a portfolio is every single item everything work item is single by itself is not it doesn't depend on another one so we don't need her article here the other parameter we can specify you the type of support that we want activate for this specific post type support are all the standard information that you inside a post of a page so the title the excerpt thumbnail custom fields and so on and so on and this way we can create another array inside this argument to specify all the values that we want so if we don't specify anything by default WordPress is going to activate the title and the content editor by default but we want something more so we want inside here for sure the title because we need a title we want the editor means that we're gonna have a text editor inside and the thing is pretty handy we need the excerpt because I want to create a preview text we need the thumbnail because of course my portfolio has to have a nice preview image for every single item everything work so I need a thumbnail and I want to save I want activate also the revision the revisions will automatically save previous versions inside our database so we can always recover different versions of that specific post type we have other parameter years we can specify the outer Trackbacks comments custom field post format page attributes and so on so on but in my case we don't need it right now I don't want to specify it but you have the option to activate other options at our attribute spa let's keep going the other parameter I have the ability to activate taxonomies by default taxonomies are activated with the same logic of the capability type we create so in our case we have post and the taxonomy of a post are the categories and the post tags so we're gonna activate dose that's tsunamis is also here an array of arguments the first one is category this second one is post oops as a string always post tags oops post tag singular comma the other parameter is the menu position so the menu position is just a numeric value to specify the location of my post type inside my admin panel so if I feel like 0 is going to be on top of everything the more I go down the more I increase the number the lower is going to be the position I want to use 5 so it's going to be like somewhere here maybe after pages of after comment let's put many position equal 5 comma we can exclude underscore from search so every time a user does a search in our website we have the ability to exclude this specific post type from the results page so if we want the user that it doesn't matter where he decided to do the research he is gonna have just the results of the blog page or posts or pages or other things other than the portfolio we can specify a true but in my case I want the portfolio to be visible inside a results page even if you're not searching for portfolio item so I'm going to put false I don't want these element to be excluded form the search results and that's it these are basically the most important parameters that you can specify there are other parameters that I omitted but they're like the default value most of the time I think like 99% of the time are ok for whatever custom post type unit but if you have doubts and you need like a more in-depth analysis of the custom post type I suggest you to go check the codex of wordpress every single attributes is listed with a way better explanation than one that it did so now we have the two variables are filled we have labeled and the argue and the labels variable is inside the arguments variable so now we have to actually register like create the new custom post type to do that we have to call the hook of WordPress called register underscore post underscore type and here we have to specify two simple values the first value is the slug the custom name of our portfolio ders is going to be used pretty much everywhere inside every URL and Indian ministration panel also has to be lowercase and without spaces so portfolio and the second parameter is the variable that contains all the arguments of my custom post type so in my case is args save it and now we have to create a hook to call the function awesome custom post type so when are we going to be able to call this to create a new post type register post type we have to add the action during the initialization of the theme so if we roll back here and we go add action we already have an init thing so we could put the function all the thing inside the awesome theme set up because our action is going to call the initialization the creation of the custom post type at the same time but it doesn't matter really we can keep it separated and I actually like it better to keep it separated so it's not confusing to have multiple hooks inside a single function and that could be kind of confusing so let's keep everything separated but we know that we have to call the register post type during the initialization of the theme so let's call the ad underscore action like we already know the first parameter as a string is when this function has to be called and then we can print the function as a string so my function is the awesome custom post type here whoo semicolon save it let's go back in our administration panel reload and if we scroll down we check on the sidebar we have the portfolio the new custom post type that by default has the same icon of the posts because we grabbed the capability of the post type we have also the ability to customize this but we're gonna see it in a next lesson and as you know this year all the labels are exactly how we specified that how we specify them in our functions PHP so we have all items of course here is no items found we have add item and let's take a look if I want to change this item with portfolio or like portfolio work I can do that on the fly so instead of add item I can say add portfolio work save let's go back refresh and that's it the label changed right away now this is pretty I also wrote word instead of work but whatever that was just an example it's pretty ugly so let's roll back to add item I like it better okay let's go back here and refresh if we click on add item we're gonna have the exact same functionalities of a post so we have the exact same categories we have the title and the text editor and if we write a title for example like logo for business a and we go down here you notice here the permalink is auto-generated with the custom slug portfolio that is exactly the same as the custom name that we specified inside will register post types so this is really good for SEO and indeed so let's pick thoughtfully the name of our custom post type and if you notice here something's not right so probably I did something say because we're missing the excerpt we're missing the Futurity major but it's something they activated in my code so that's it what I did wrong is instead of support the actual word is support so as you notice if we don't specify anything because support wasn't anything WordPress was ignoring this value and was using the default information of a standard post we had a title content editor and category in tags but it wasn't actually adding the excerpt the thumbnail and the revision something that I want so let's change from support to supports save it let's go back here refresh stay on this page let me publish this refresh and as you notice here now we have the excerpt here and the future ad major gives us the ability to upload a custom image to use however we want so this is the first part of the to parcel tutorial about the custom post type we learned how to create a custom post type inside our text editor and how to customize all the items inside our administration panel of course you have the ability to create it how many post Styles you want inside the same function you can specify again another type of variable like labels one or arguments one and create another post type customize for your needs we're going to see how to actually print the portfolio information the custom post type in our front-end and now to completely customize the experience and to separate these entire results from the blog results page or the other custom pages that we created so be sure to keep following and subscribing to the channel and wait please wait a little bit more for the second part of this lesson about custom post types so thank you again for checking this video if you liked it please give it a thumbs up or subscribe to my channel and I hope to see you guys or hear some comments for you for this lesson and to see you all in the next lesson where we're going to complete these part about custom post types so thank you again and see you soon
Info
Channel: Alessandro Castellani
Views: 119,630
Rating: undefined out of 5
Keywords: Wordpress, tutorial, help, web developer, php, how to
Id: vSM7w3zzlSU
Channel Id: undefined
Length: 28min 29sec (1709 seconds)
Published: Sun Aug 30 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.