 We'll come back to the channel, so someone sent in a challenge and that's for me to create a dashboard sort of like a report for the most trending post that's being seen. Also we are thinking maybe also get the post that has the most comments. So basically they are looking at seeing where their traffic is coming from and how they can grow it and how they can actually see that it is proper. Now I'm going to do this by starting off by just going to my plugins folder. I'm just going to drag that and drop it in my editor and I'll add it to this workspace. So what I'm going to do is I'm just going to create a new file, give it the same name and then I'll add on PHP to make sure the extension is right. And I'll just copy what I have here, this other plugin and I'll just drop it in here. So all we need to do is change this and call it a trending post widgets information for the designer or for the content of content maker. So when we save this, we actually have, if we go back to our plugins, we'll see that it's already here so what we need to do is just activate it. Now the first thing that I'm going to do is that I'm going to go to our posts just to see what posts we have there. So we have two posts here, demo post to and hello world. And what we're going to do is that we're going to use some of the inbuilt methods of what WordPress is. And we're going to add sort of like a counter but it's going to be metadata. Now WordPress custom post types or posts themselves or the post types allow us to store metadata along the post. As long as you have the ID you can store attachable small details of information. Now we're going to leverage that to make this work. So what we're going to do first is I'm going to create a function. And then I'll give it our name and I'll call it TPW for short trending post widgets. And I'll say we're going to call this post single post views and that will be our function name. And we'll call this to do whatever it needs to do. Now of course we're going to add a meta key. That is the name of the key that we shall have for the particular metadata that we're going to store. And we're going to just call it, finally I can get my editor to work. So we're going to call it TPW post views. Let me make some corrections here. I'll just call this save so that it's not confusing. So I'll call it TPW save post. Now we are going to save a meta key on our post. And the way we can do that is we shall be able to query for this metadata at the end of the day. So let's say we call this views and now we're going to be querying the views. So we use the get meta, get post meta function that is in WordPress natively. So our function requires three arguments. So the first one is the post ID because this data is going to be saved on each individual ID. And then we're going to add the meta key that we are saving. And then we're going to say this should be true so that we only return it as a string of information not as an array. So we'll pass it on to be true. Now what we can do for now is that we can even echo this out so that we see what actually happens. So we're going to save this. And the way we are going to tap this method is that we're going to use what we call a hook. So we're going to use a hook which is called WP that is automatically run every time the WordPress query is run. So every time a WordPress post is run, it runs a WP query against the database to bring in that post. So what we're going to do is we're going to run this action. And then we're going to add our function which we have already saved here. So I'm going to add those and then save this. Now we just want this to actually run, okay, era cleared. We want to know if the page is actually is single. That's when we want to run that particular file. So we'll only do it on posts and not any other post types. So we want it to be if it's single, so we need to wrap our condition. So if it's single, it will be a single page, then we'll run this. So what I'm going to do here is before we run to see because I know we're going to get an error. Let's say if I run this here, we'll see nothing is going to show up because it's not locked up. So what I'm going to do here is I'll wrap this also in some proper HTML tags to allow us to see something that is... Let's use something like an H2 to make it very obvious. H2, I need to close this off. And then what we're going to do is we know this particular views is not going to exist because it's not created. Now what we need to know is that every post is going to start from zero. So we want to say every time we count the post, every time we count the post, if it is empty. So we'll write this in a short form. If it is empty, then if what is empty, if the views is empty, if views is empty, what we're going to do is we're going to say let it be zero. Else, let it return views. So that's the way we're going to do that. And then we add the semicolon. And if that happens, that is okay. So every other time we shall want the post count to automatically add on another. Every time we view the particular post, we'll want it to have the post count and add on another post. And that's how we Google and all the other systems see it. So what we're going to do is that now we're going to use... If we have here getPostMeta, all we're going to do now is actually going to do updatePostMeta. So we're going to update the PostMeta, which is also an inbuilt function over WordPress. And I'm just going to copy this here and add it because the same information we need. It's need the post ID, we need the meta key where we're going to save our data. And actually the final thing that we do need is actually this information. We need to save our value of the post count. So after doing that, we shall query getPostMeta and then we shall echo it out in an H2. So what we're going to do is come back to this and then I'll reload here and nothing is happening. I think because... Let me just comment this out and save this. We'll reload this. We shall see this page is loaded and we don't see any H1. When we are making this function, we need not pass in the ID. So we need to pass in the post ID. Post ID because when we are passing this particular action, we shall have the post ID. So I'll just edit this and then save this. Then I'll just uncomment this out and save. So let's reload this demo post. And then we'll see... It still doesn't do anything. I'll cover this out. Introduce it a little later. Reload. Our page is not showing anything here. So let me just try this out and stop. Let me first save this action. And then I'll just try to view this in my theme. My current theme is 2020. I'll drop it here in this space. And I'm going to look for the singular.php and say if we have post while have the post, I'll run this function and save. Let's see if we can actually echo this out. We see it has few arguments. Zero is passed in 9.23 and exactly one is needed. So we're just passing the ID, the post ID, depending on how it was saved. So if we have the post, we just do get the ID, which is the function that we need. And then I'll just close this off, come back reload. And we don't see anything in our post. We are looping. We have an empty h2 here that's coming. It's because views, which is our query, is actually not returning anything yet. So if it's empty, then that's zero. Then update post ID. Let me just echo this. Get post meta. Then I'll pass in the post ID. Get the ID, which is what we need, comma. And then pass in this meta key. Save that. I'll commit this out, save this. So let's see what this gives us. So I'll reload this. Anything, do it. This here. And this here. Where this off. Save. Let's see what happens. Yeah, we have one. If we reload the page, it's still one because it's not running the function. Then of course we'll be running this function because it's here. So every time we reload this page, we'll actually see one coming. Reload. Now it's two. Reload. Now it's three. Reload. Now it's recording. Every time we change our page, this is changing. Now of course we don't want to affect the other pages. So what I'm going to do is a number of times whenever you call a particular page. You see that it has this linked. It has those linked to it. So every time we call, we're getting this to become four. If we go back to hello world, we'll see now it becomes six because it was called twice. Initially it was four and we call it chem five. Now what we need to do here is that the first thing we need to do is we're going to remove action. And in wordpress there is a particular hook that works with this. And that's in the WP, in the WP head, we're going to get the function which is adjacent. Adjacent. Posts. Rail. Link. WP. Head. This has to be an underscore. So this is what we're going to remove. We're going to remove adjacent. Posts. Rail. Link. WP. Head. So I'll save this and let's see what happens. So every time I reload this page, when I reload, this will become seven. That's hello world. Remember our demo post was four, six. It's now seven. Let's go to our hello world. It's eight. We're leaving it at nine. Let's go back to demo post which was seven. Let's see. Seven. Now is eight. And that is okay. Now is nine. If we go back to hello world which was eight. Which is sorry was nine is now ten. If we reload it, it's eleven. Let's go back to demo post. Is now ten. Eleven. This is twelve because it was initially eleven. So we're actually doing well. Now what we need to do is that we need to run our faction outside of our themes. So what we need to do is that we need to find a hook that's going to give us both the ID and so that we can run our particular functions. So we'll just leave this to show but we're going to cut this out and save this. And then we're going to come back and run. So we need to move this out and then comment it. So we need to find a proper hook for this. This is not actually working for us. So I'm going to try another hook which is a loop underscore starts. This particular hook loop start actually passes one argument which is a it passes in the query. So it passes in the query. So once we get the query we shall be able to get our post ID. So if is query. Now we're going to look if this is the main query of what we're looking for. So we'll do the if main query then we'll run our code. So what we need to do is say if query is main query. That's what we're going to do if is main query and it's a function. What we're going to do is we're going to overdump what the main query is. So we just need to dump query in here. So and then what we shall do is die to just see what's happening. So reload this itself. So we're getting it which is good. So the query have the page, the query vars, parent post, the sub post, the sub post. But what we're looking for is ID. So let me find ID and we have 16 matches. So we have ID which is the integer here. So we are looking for the ID. So query dash ID. So we're going to move all our code in here into this. And then what we're going to do is we're going to actually make this our variable. And say post ID is equal to query. And then we shall get ID on this query and the ID. That's what we wanted. And then we shall delete this and save. So coming back to this page if we reload before we had 13 fingers crossed. We have 13 and it's still 13. No, not working. So let me just overdamp this again. Let me overdamp that and then I'm actually going to overdamp the post ID because we want to see what we are storing in our data. Copy, paste this and take this out. Save, reload. Ah, we're storing now. Let's just go back and do this again. So query, we're going to get the query, paste it here. And then we shall echo some pre tags to see exactly what we are working with. Okay, so get this, cut it, put it here. Duplicate this, move it down. And then close it off. Save. Let's reload and see what we have here. So we have our object which is query. We get the page, the year, the string, the month, the day. Then we have query verse which gives us the na-da-da-sup post. Page name. Then we have page ID which is an integer zero. And then we're still looking through to find out what we need. It's giving us now queries, debt queries. Yeah, so we are looking for queried object. And then queried object is going to give us something different. So let's overdamp that. Chain, queried object, reload. We get the query and then we're going to get the ID from that. So let's chain the ID from that, save, reload. And it's an integer one. This is for hello world. Let's see for demo post what we have, integer 18. So that's the right ID we're actually looking for. So we just need to pass this on as our ID. So do this, take this out and then save. Let's see what comes back. So if we reload this page, it's now 13. Reload it 14. The reason that is getting displayed is because we have this showing up query H2. I'll call this class meta key. Save it so that we can differentiate what's going on. So we reload this. We have that. So 14. Meta key is showing us 15. We're having H2 of views here as 14. And the reason why that is showing that way is because this has already queried for the views previously and it's just echoing it before. It's not calling this loop again per se. So what we're going to do is just remove this and so we have our function that's actually going to, it's going to save what we need in there. Now, of course, this is going to save for every post and every page that we do have. So whenever I reload, I have, before it was 15, now it is 16. That is demo post is in 16. Let's see what hello world is at. It's at 14. If I reload 15, go to demo post which was previously 16. It's 17. So we're good to go. We're saving what we actually wanted. So after a number of minutes we've been able to sort this out. So what we're going to do now is we're actually going to develop our dashboard and what I'm going to do is I'm just going to copy what we had here and I'm going to create a new dashboard. So I'm going to copy this, come back to our widget. So I'm just copy this code from some other widgets that I had made in an earlier series, you can see the video linked here. Up at the top, you can see the video linked and we're going to create a dashboard here which we shall call trending, trending posts, widget and then we're going to just call this trending posts and so it's going to run a function that's going to query for the different information that we do have. So we shall call this trending posts query which is the function that we want to have. So we'll call this function posts and then we'll do this. The next thing that we're going to do is actually query for it so what we need to do is we're going to have a number of arguments that we're going to pass down into a new loop which is a new WP query and then we shall echo out that information. So this is what we're going to do fast. We're going to get posts, ads which are arguments and there will be an array of information. So what do we need? We need the post type. So the post type which should be post and then we're going to look for posts per page. So we'll just wrap this here, posts per page and we're going to limit that to let's say five posts for now or ten. Let's just eight, let's go for eight and then what we're going to do is we want to query basing on the meta key. So we shall add the meta key that we want to look for and the meta key that we are looking for is called so that's the meta key we are looking for and then we are going to add that and say meta value should be descending meaning from that top to the lowest. So we'll save that. Now what we're going to do is we're going to add a query say is equal to new WP query now that new WP query that WP query we are going to pass the post ads in the new WP query we're actually going to this is lower letters query the thing that we're going to do now is passing the argument which is the post ads that's what we need to throw in our query and when I look at this I actually see I made an error what we're going to do is when we get the meta key the next thing we're going to do is we're going to order it by so we're going to order by this order by and we shall say meta value number and then comma I'll duplicate this and then what we need to do is order this and say the order we need is actually descending so we'll come from the highest to the lowest so we have a new query here now what we need to do is actually just complete our code so what we're going to do is just come and say if our query has posts so we say have posts if we have that in our query then we're going to actually just echo those particular things so I'll just echo this so what's missing is after we have if we do have then we need to do a loop and choose while we have a query we need to end the while also so while we have our post type has that query we need to get the title and let's see how this plays out so we'll save this make corrections, we've made corrections so while we are looping through our content because it was true let's see what comes back so if our post has query if our query has posts and we go for while it has posts while it has posts this is what we should do we should echo out a URL we need to move this in while it has that and then we need to move this out because we don't want to endlessly loop some our HTML so this needs to go up so end while so we'll echo our URL and while we have posts we shall echo each individual post in an LI post title in a list element and then we shall close this off so if our query has posts we echo out a URL and close one off again inside we shall do a while loop whereby we just query out the s query if the query has posts then we have to do the query and then we shall tag on the post and then after that we are able to just loop through our titles just using the LIs and then at the end we have to reset by using the wp reset post data to make sure that we are not doing a loop through the data again so we're going to reload this and when we reload actually have our demo post showing up here and we have Hello World now the other thing that we could attach to this is actually we can add a span tag and then close off that span and what we are going to echo in there is the number is the number of so we're just going to go back here and get a post query and copy this here because we want to show the number of let's say the number of times the post has been seen and so here we're going to actually change this to say id and I'm going to pass the meta key here and so when I save this and come back and reload we actually have 20 times this has been viewed 16 times so what I'm going to do is I'm going to reload our Hello World again and again and again and again and again so when we come back and reload here in our dashboard we're going to realize that our Hello World has been loaded 18 times if I reload it one more time and come back here you'll see that it is now 19 I reload it one more time another time and another time so that it goes above the demo post too so you're going to see that it actually switches so we have our Hello World at 22 we have our demo post at 20 you can style this but at least we've managed to make a step in the right direction so if you've enjoyed this video please give it a thumbs up and then follow up for the next video that will be coming we've been able to show the number depending if there were 10 posts all we need to do is actually just edit this here to query to make it 10 posts but right now on our page we have only 2 posts you'll be able to see those 2 posts and so the next thing that in the next video I'll be showing you how we can actually work on having the posts that do have the most number of comments the good thing is that our WordPress already has that covered for us because they record the comments on each post so we shall be able to pick that out and then just throw it in a dashboard if you enjoyed this video please give it a thumbs up if you like to know what's coming up click the bell so that you don't miss anything new that's coming out