 So in today's video I'm going to try to attempt a challenge to see how I would have solved this on my own. One of the viewers asks about a custom post type of a course and says, can I make two post types that are related? One of course and another of lessons and I could complicate it a little bit by adding modules and there are two ways of looking at this. So let me go inside my WordPress. Now WordPress offers us something that's called custom post types and if I wasn't going to do something that's very complex in terms of something like a management system where you can use things like land dash or lifter elements I could simply go for a custom post type and then maybe I would add in some custom taxonomies for me to do that and what I mean simply is let's say if I come to posts here posts but you couldn't make your own post and which is a custom post type and you could have taxonomies where you have something like these categories right here. So let me try to jump into code so that I can show you what exactly mean. So inside my root folder of my website I'm going to go to wp content I'll go into the plugins and inside here I'm going to create a new folder which I'll call custom lms and I'm going to drag and drop this into my editor and inside my editor I'll start by adding the custom dash lms file and this is a PHP because we're going to write it for WordPress and WordPress mainly works with PHP on the server side I'll start with PHP here and then I'll start off with my start plugin this is a code snippet I always add when I want to start plugins license I'll give it up a gpl I'll give it a custom plugin name and I'll call it a custom lms and then I'll change the links put in my domain I'll plugin author I'll just put my brand name here and then I'll say custom lms or simple custom lms simple custom lms I'll just change the name of this right here as well and then everything else here will just be simple custom dash lms now first thing is I'll add some simple security by saying is defined we're going to find out if the app's path is actually defined and this can only be found inside WordPress and if someone tries to access this particular file of yours by hitting let's say the domain URL and try to get to this file to run its code it will not allow them so you're simply saying that decision needs to come from WordPress to run this plugin and not from an outside code that will help us get to this level now the first thing I'm going to do is add a custom post type and the custom post type I'm going to be adding is for courses so it's called courses now inside here I'll add a custom taxonomy I'll add a custom taxonomy called module and then finally inside those individual posts as well create the different lessons so this is quite easy I'll just come here and google this and so I'll say custom post type say custom post type WordPress codex and you'll see we have this function of register post type that we have to use now let me go and just pick the sample code and we have this method of WordPress which is called register post type where we pass in the post type and a couple of array items now these parameters that go in the array are the labels the public the description making it hierarchical can exclude from such we can make it publicly queryable we can show it in the UI in the menu and all these other things and even showing it in the rest API right here that will allow us to use things like Gutenberg if we want to and then just adding capability do we want this to only be for the administrators or should subscribers and other people be seeing this so there's so many options that you can choose from here to write your custom post type but what I'm going to do here on this page is scroll all the way down so that I can see just a sample usage of this particular post type so I'll copy this code it's a mang one of the highly recognized comments come back here in my code and I'll paste it then I'll just run through for you what's going on so on the add action we are using the init hook so that when wordpress is initialized we run this function that actually starts from here now remember we talked about having the register post type function running here so in here we're running a post type of recipe which I am going to change for all these different times it appears I'm going to change the word recipe and I'm going to call that course so that I'm registering a number of courses and then in the arguments I'm going to pass in this number of arguments so I pass in the labels which labels is also a value that we've stated here so we have that name the name of the courses which is courses we have the singular name for course the menu name will be courses we'll add it to that mean bar to add course we'll add the add new add new item editing and all the different things down here that we need so I'll save this come back to my admin side I'll reload here and you'll see that our plugin is available and then I'll click activate when I activate it you'll see that we have here courses showing up and we're able to add a new course if we want to so I'm going to call this English so I'll call this course English now the reason I can use it in Gutenberg is because I added it to show in rest now if I want to use only the old editor or the classic editor on this I need to show in rest as false instead of true so I'll save this as false come back here reload and you're going to see that we have the old editor just showing up because we've denied it to be shown in rest and that's the way Gutenberg works it works with the rest API so I'll go back to my code here and I'm only going to support let's say the editor the author I'll allow a thumbnail so that we can have a picture at the end of the day so if there are these options that you have to look into and we usually get the basic of this so it's not hierarchical we're giving it a position of 20 we're giving it supports and we're going to create our own custom taxonomy so I'm going to remove this category and post tag and we shall create our own custom taxonomy showing up here so you see right here we're having categories showing up so when I reload here we don't have category showing up anymore so we're going to create our own category which we shall call modules so I can come back here and then scroll all the way up I just need to find the search and I'm going to say look for custom taxonomy hit enter and you will see that we also have another function called register taxonomy now when register the taxonomy here it will ask for the taxonomy name and then we'll have other arguments that we're passing for example we want to have an object type we want it to be of an array or do we want it to just be a string then we add particular variables like the label the description should it be publicly queryable should we show it in the UI should we show it in the rest we are doing all these things and adding them so I'll just scroll again down and then we're going to look for an implementation like what we have here and this is sort of going to help so I'm going to copy this come here and paste it and we have two taxonomies that are being registered so what I'm going to do is just remove one of these so we use the init hook again of wordpress to run this function and we give it a priority of zero so that it fires really early now when we come to the function that we are calling right here in the hook we see that we have labels and these labels are name like genre singular is genre we add text for when it's going to be searched and all of these things then we add a menu name and then we add whether it should be hierarchical we add whether it should have labels should it show in the UI should we have an admin column for this should we rewrite it and we are going to rewrite it with a slag of genre but I don't want to have this as genre remember we say we're going to have module in its place so I'll change this to module but we want this to show in a particular post type and the post type we want this to show up in is actually the post that post type that we made here so the post type we want to have here is course so I'll copy what we have here as course and change it for book so we have registered a new module we want it to be part of the course and we've run its different argument so let me save this come back to our backend I'm going to reload here and you will see that we have modules showing up here now of course we can change this it's not looking so nice in our menu and we're going to call it course module I'll change this to uppercase and I'll change all these other ones module right here change this to uppercase so that we have it proper and looking nice I'll save this come back here reload and you will see that we have course module here and when I click on it I am now able to add new modules for example I can say our course is going to be English and the module I'm talking about is comprehension so that's the module work I'll save this as a module I'll come back to my courses here and I'm going to go into the English course and you're going to see that we have a module showing up right here so when I click on this and hit update you're going to see that I can classify this particular course with its module and that's as far deep as possible as I can go I cannot write individual lessons to it but we can look at this in the reverse so let's say if I created another taxonomy and that taxonomy I am going to call it courses and then I'll change my post type to be a lesson so we are sort of reversing the outlook of everything so let me go back into our code we're going to change this course into lesson and then we're going to change our custom post type from courses to lesson so let me look for course I'm going to change that to lesson we'll allow modules to be in there and then I'm going to also duplicate this same thing here I'll have to unset levels because I've used it up and I don't want it to become a problem again down here I'll also unset the args because I'm using it down here again so args so I'll unset those two in the second place where we're using module I'll change this to course make this lowercase c lowercase c save this come back here I'll reload and we're going to see that this is broken because we no longer have that post type and I'll hit my dashboard here and you're going to see we have lessons and inside lessons we have lesson module and we have lesson course now whenever we add a new lesson let's say we're going to call this lesson one we can add a module it's going to be in comprehension and the course is going to be English so when we save this we can be able to query this by course then by module and then by lesson so we can query this on the front end or on the rest api coming from it backwards so we start from course and say let's get all the courses coming in let's get all the modules and then let's get all the lessons that are as per each individual taxonomy there is another way of doing this and that's by using a plugin called advanced custom field now advanced custom fields written by eliot kondon is one of the most powerful plugins there and it has over one million installs i'm going to just install it and i'm going to show you a couple of things about it that are just really really advanced and super so i'll activate the plugin now and once i have it in here i'm going to deactivate the simple custom lms for now let's see what's happening in here so we have a new menu item here which is called custom fields and in here we can add a new field and this field can be called custom lms fields now it can be tied to a particular post type if that's what you want to do to have that and what we're going to do is we are going to go back to our custom lms plugin and we are going to do something different for now so i'm going to go back to my plugins still i'll activate custom simple lms and instead of lessons in here i'm going to comment out everything and just create new posts so i'll copy this code that helps us to make new posts and i'm going to comment out everything else so save this so there is no code that's active i'm just going to create some new post types and for example this post type that i have here of lessons i'll have that which is okay i'm going to create another post type i'll need to change this name and then i'll call this create modules modules cpt i'm also going to change what we have here and i'm going to create some other cpt for a custom post type which i'll i'll just go for this let me change this from modules to pt look for the one down make sure i'm changing this and calling this lessons cpt so i'll save this come back and change what i need to change here so we have this cpt right here which we are going to call courses create underscore courses cpt so i'll change this lessons into courses i'll change every instance that we have here of lesson so we're going to have the first one and it's going to be courses courses which is okay the next one that we'll have is going to be modules so we need to change this lesson into modules so we have our courses up here need to change this so we have our courses up here we then have down here modules and then after that we have our lessons here so let me save this come back to that main area reload and you're going to see we have courses we have modules we have lessons these are all different items that we can use now in the individual custom fields i'm going to just publish this and then so that we can be able to now see our new modules courses and lessons showing up here so i'm going to type particular fields to the individual post types okay so in the post type of our lessons i'm going to add a couple of fields so in here we can have the teacher's name and we have that that will be a text it will be required update that and when we go back to our lessons so i'm going to come to lessons and when you look at lesson one it already has teachers right here so what i'm going to do is try to attach the different modules and courses into this lesson so i'll go back to the fields i'm going to go back to custom fields click edit and i'm going to add the connection between the module and the different lessons so in here the field that i want is going to be relational and it's going to be a relationship so in this relationship i want it to be required and i want it to be filtered by one particular post type and which is modules so i will allow the filters of such post type and taxonomy so i don't need taxonomy i remove the post type because it's only going to be for one post type which is modules so what i will do here is finish this all up and hit update now when i come back to one lesson go back to the lesson here you will see that we have teachers and then we can set this in a particular module now the reason this is empty is because we don't have modules as yet so let me create some dummy modules i'll create this as comprehension click publish add a new one i'll call this a composition publish and then i'll also come to courses and i'm going to create a new course which i'll call English so click save now whenever we're making lessons here we can come to our lesson we add the teacher the teacher is going to be take a press and then we can add the module and our module is going to be composition we can also add comprehension if we want but we don't want this to be part of both modules so i'll just leave on the one module here so i'll click this minus to remove that and then the other thing that i want to do is add a particular course to this so i'll hit update here so that we don't lose that data i'll come back to my fields come back to this and then i'm going to create a course so that we know each lesson is attached to a module and that module is probably attached to a course or i can do the reverse so course and i'll say course is that it's going to be relational in nature it's going to be a relationship i'll require it and the post type this should go is to courses i'll remove the taxonomy because i don't need that and the post type and i'm going to come here and update so when we go back to our lessons here and get our lesson one we can attach our lesson one to the composition module and we can attach it to the English module so when i save this i can be able to query the different lessons and then categorize them basing on the different fields that i've added here so we can now query for this lesson and we are sure that it will have the module and it will have the course added to it so this is how i would probably do it i'm sure that it's working fine using a plugin like advanced custom fields to determine that all my points of information are actually showing up compared to using a taxonomy and the reason is sef would allow me to add boxes like this of information to each individual post type so with courses i can add different details of metadata just like with the modules and just like with the lessons itself here so with even with this lesson i can add a field so if i come back here to fields and then go to i want to edit this lms field so i'm going to change this to lesson fields so that it's very clear i'm going to add a field here which i'm going to call document so it may be this document is something that i want to upload work so i want to upload a document so i can come here choose a different type and in here i want to have upload and i'll choose file here so that it will allow me to upload a file and what i want to do is return a file url not just an array maybe i want to get the array or file url whatever it is and i'll add it to upload it to post and then i can add here limit for the file sizes minimum to zero and then maximum to 300 mb and then i'll come update this document and when i come back to my lessons here i'm able to look down all the way and i can add a document from anywhere my computer or wherever and it is working fine so this would be a better option for me to just use scf to create my own learning management system and give it the different fields that i need to do that