 Hello, good morning, everyone. It's really nice to be here at Singapore WordCamp, and this is my first WordCamp speaking. I hope you are ready to get to know a bit about custom post type and extend your WordPress project to next level. I'm Dasun from Sri Lanka. Currently, I'm working as a lead UX designer in John Keel's computer services. Before we move into the session, I'd like to tell you a bit about me and WordPress. I started to use WordPress from 2011. I used it as my university project. Then it became my hobby, my career, and basically, I'm obsessed with WordPress. So I think any software can be done using WordPress. So I also, I've been working in a software industry for six years. I've been doing UX and a lot of stuff to e-commerce, airline industries, leisure, education, and those stuff. So I firmly believe that we can use WordPress for any kind of application development regardless of its domain and size. Being said that, I have outlined my session in the following. First, I'm going to talk about the default post types in WordPress. Then we will see where we can use custom post type in our WordPress projects. So then we can see how we can create a custom post type. Then there's some other functionality to extend custom post types. So then finally, we can see how we can get data on the front end and finish the applications. Do you know what is post type in WordPress? Post type in WordPress is a way that WordPress can hold data, not all of it. But there are many ways that WordPress can hold data. So post type is one way. There are five different default post type in WordPress. First one is post, which is typically used for the blogging. And it's displayed in reverse order. And post can be assigned to taxonomy. Also, there's another thing called post formats. And the second one is page. Page holds another post type, which we can use to display static data in our websites. Page can be assigned to different page template, which is extremely useful when you are looking for an extent in your applications. And by default, page does not have these taxonomy features. And another important thing is page is hierarchical. That means it can have parent page, child page, so on. And the third one is revisions. It's a post type that's related to page and post content. And revisions are used to hold drag force, as well as the fast revision of a published post of page. Then the fourth one is attachment. If you upload a file via WordPress media uploader, it will create a post called attachment. It gives the data of your images, or sizes, and those stuffs. And the final one is menus. It is a type that holds information about navigation items. It is the only type that accept the blog post that we are storing in WP post to navigate throughout the WordPress system. OK. If you are going to develop a custom post type, it's really important that you have understand these default post types. Because if you are going to build a custom post type, that doing the same functionality, that have the same feature as a default post type, so there's no point of developing a custom post type. So you can use default post type in such a way. It's really good to know. It would be better if you have better understanding about default post types in WordPress. There's a many situation that we need to use custom post type. I have taken some examples. I assume that there's a creative agency and you wanted to display your portfolio. There's a way that you can use default post types. You can create functions called short quotes. Or you can use default post using taxonomies. But it would be nicer if you have a separate interface where you can add your project data, such as project videos and project images. And you can have different project categories. Again, if you take another example like job vacancy site, you can create vacancies. And you can create different taxonomies like programming and IT accounting, those types. So this kind of situation, we can use WordPress, custom post type, to extend our current web applications. And I have a prepared small demo for this tracking application. If time permits us, I will show you at the end. It's like a web application. We can use WordPress for the web applications as well. So if you take this tracking application situation, let's say that you want to track application. If you ordered something in eBay or Amazon, you don't know where your application, where your product is. You need to track the status. So using WordPress and custom post types, we can create a small tracking application. So what we need is we need to information about the item or the product. And we need to track the status. And maybe leave a comment or give a rating. We can communicate in between. So let's see what are the ingredients in the WordPress. So we will need a custom post type cold tracking item where we can add data. And we can register a few custom post status using WordPress functions, like in transits or shipped or local post and received whatever status that you want to add. And then we can add some custom taxonomies to categorize our product, whether it's a book or it's an electronic item. And some metadata to extend, give more data about the items. Finally, we can get the data into the front end and manipulate, finish the application. OK, now we will see the custom post types. If we are talking the term custom in the WordPress, that means that everything that we are hooking outside to the system. I like to use the term hoax because action hoax and filter hoax are the way that WordPress can extend them. It is the easy way. So in the custom post type, we will see how we can register custom post type. And then we will see extending post type with custom taxonomies and meta boxes. And we will see what are the best practice development of custom post type. Custom post type is a way that you can custom content. As I said earlier, it's like movies, product, portfolios, whatever the custom content that you feel you need to add to your WordPress applications. If we talk in SQL way, it's an additional value in the WP post table in post type column. I found this here Lego. I think I can explain. I feel it's a good example how we can explain WordPress and custom post type. If you can see this base Lego, that means foundation and base, it's like WordPress. You need a special way to hook using these building blocks. If you take building blocks as a custom post type and you can build anything top upon this Lego, there's a way. So it's like custom post type and WordPress. So this is the way that WordPress can create a custom post type. It's really simple function. In the top up, there's this add action. It is the hope that we can hook custom post type into the WordPress. And there are a few arguments that we need to pass. These are the required arguments. And these are the main two arguments that we need to pass with the registering custom post type. First one is label, which defines our custom post type name in singular and plural way. And we need to define whether it's a public or not. If it is a public, you can see the custom post type in your admin panel. If you define it's a not public, I mean public. This is a Boolean through or false. If you say it false, it will not display in the admin. Example, for example, the revision, it is not display in admin menu, but you can get the data. It's like that. If you want to create some auto-generated post type, you can say it's as a public fails. So this is the expanded version of the registering custom post type function. You can use this function to give more uniqueness to your custom post type. Sorry. You can use these arguments that are labels. And you can give a small description using description. And there's another very arguments to pass more labels. That means you can customize even more. And these supports are the things that WordPress backend will create some data in certain fields like title, editor, and images. And again, there are a lot of arguments that you can pass that mean more arguments mean that more you can customize. You can even define URL types and menu icons, and there's a lot of stuff. If you refer the codecs, there are very detailed explanations in each of every item. So now we will see how we can extend these custom post types into even more. You can register custom post type. Remember the tracking app that we talked in few slides back? We need to register few status, like shift, dispatch, or entrances, those kind of stuff. So it is the one way that you can extend custom post type using different custom status. There are eight default status in the WordPress. But using a register post status function, you can register more custom post types, but the thing is, but if you register custom post type using this function, it won't display in your WordPress backend. I have submitted a bug to that one. It's really going, we can hope in the future we can have that feature available in the WordPress. But there are a lot of ways that you can get this display in the backend. So after adding the status, now let's assume that you want to add more data to your custom post type. That means you already have WordPress fields, like editor, expert, and images. And you want to add more data. So you can use metadata. WordPress has a very good API called Metadata API. And there are two types of metadata, which is user meta and post meta. We can use post meta to add custom meta books to our custom post type. If you are going to add meta books to your custom post type, you need to follow these steps. And there are separate functions for each of these. Plus you need to add the metadata. Then you need to add a function to update metadata. Finally, you need to delete metadata and get data into the front end. This is adding custom taxonomy. There are some scenarios that we need to have a different taxonomies than what we have already in WordPress. If you're creating custom post type called movies then you need to add movie types. And if you're creating custom post type for the books and you need to add book types and so on, there's some situation that we need to use custom taxonomies. And this is the exact function that we are using to define custom post custom taxonomy into WordPress. So here you can define your action hook and the function that related to hook. And you can define the post type and type for your label. And you can even define your custom taxonomy. And this hierarchical thing is important. There are two types of taxonomy, like tags and category. Tag is non-hierarchical. That means there's no parent and child thing. If you define this as a true, your new custom taxonomy will be like hierarchical. You can have a parent and a child like that. And when you are developing a custom post type, there are some best practices that you need to follow. It's always recommended that you use custom post type with the plugin. You can create a custom post type, hooking that into the WordPress theme as well. But if you always think that you are developing something for the end user, and if he changed the theme, that all the things will be lost that you created in the custom post type. So it's better you always use the custom post type in plugin. The second thing is you need to always prefix your custom post type or the functions that you are creating in that plugin. Because if someone created the same plugin, no same function, same custom post type, and it won't conflict if you define your own prefix. The second thing is if you use a prefix, that won't be nice in the front end. So you can use URL friendly methods that give you a better URLs. And you can add more customization. That means you can add more uniqueness to your custom post type using earlier arguments and these things. So it will give you a unique identity to your custom post type. So these are the things. And finally, we need to get this data out of the front end in order to complete the custom post type application. So normally we use WP query class to get data out of the front end. So we can use this WP query class in two ways. One is short code. That means you can define a short code and inside the short code function, you can use WP query and loop through the query and get the data. And if you're created a custom post type like single pages and those stuff, so you can use WordPress templating, which is you can have your own template, your custom post type. So it is easy, creating custom post type and getting data. It's everything like a template that already defined. You just need to check the terms that you need to check like custom post type names and slug and icon those things. And you can create a custom post type and custom taxonomies within a couple of minutes. So getting to the data in the front end is the things that you need to have some coding, knowledge and I'm sure you can find many things from the internet. So before we go to the QA session, let me show you a small application that have developed using custom post type. So it's very simple and there's nothing big. I have created this tracking application based on the custom post type and the custom taxonomy and custom post status. So these are already I have entered back in like this is the tracking code DRF 40 and you click the track now and it will get the data. We found your tracking and tracking ID and this is the item in it. You can see the tracking status. So if you, this is the single view of that item. It say that status is local post and you have some small data and I will show you the back end. So this is the tracking item post type and you can add new item and you can use a custom taxonomy like item types, apparel, book or whatever the things that you need to add and you can use custom taxonomy like item types, apparel, book or whatever and this is a small description about your application and as you can see you have, we have few post status defined not found these page in transits local post and received and this is the single page template that mean we have used templating and in the home page I have used short code function I have developed short code to get this form and you know things into the front end. It's covered short code and the templating part as well so that's it's about custom post type and I want to mention that there are few good tools I always use you can use WP generator it's a generator website you can generate custom post type custom taxonomy and anything that you there are most things that you can generate admin widgets, those types and there's another plugin called metabox which allows you to create metaboxes to your custom port site it's not user friendly but it's developer friendly there's some code knowledge that you need you can add custom post type using metabox plugin and as always you can refer WordPress codex it has lot of data about everything with examples and I have added my code references here if you want to grab a code of that custom post custom tracking application you can take the code from there that's all thank you Thank you Darshan for inviting us about custom post types so take it out of the Q&A session if you guys have any questions about him he is open to questions Hi Hi Darshan So we are currently doing the documentation on some of our plugins now I wanted to know that for documentation should we use pages or should we use them as post like which one is better and why well it depends on your requirement I mean if you want to add parent document and under the parent you want to create a child document then page would be fine so it's like there's no relation between you just need to create a title and you need to add the data then post would be fine when you define the custom post type in backend you can define this whether it should be work as a post or the post page I hope that answers your question Any more questions? Anyone? Okay if you have any you can kind of read them they will be great so thank you Darshan again I hope you come for more work and please introduce yourself Thank you So our next speaker