Join Morten Rand-Hendriksen for an in-depth discussion in this video Counting views with Custom Fields, part of WordPress Developer Tips: Creating a Popular Posts Plugin.
- View Offline
- To create a popular post function, we first have to decide what metric we're going to use to measure post popularity. Because if you think about it, there're several different aspects of a post we could use to indicate popularity. You could say that the post with the highest number of comments is the most popular. Or that the post that has been shared the most through social media is the most popular. In this course, we're going to create a new metric called "views" and basically what we're going to do is count the number of times an individual post has been viewed.
That means if the post is loaded in a browser, it counts as one view. And if a person loads the post multiple times, each time that post is loaded is counted as another view. Because at least in my mind, if someone comes back to the post several times and reloads it, it means that post is interesting and probably more popular. To make this all work, we need to add a new field to each of our posts that we can call "views." And then we start the field at "0." And for every time the post is loaded, we count up by one.
So if our post is loaded 10 times, the "views" field is set to the number 10. We can do all this but in an existing field within the WordPress editor known as Custom Fields. So let me show you how that works before we start working on the function itself. If I go down to a post here, let's say this "Tools of the trade, 2014 edition" post, and I go to edit the post, I can open my "Screen Options," and look for a box that says "Custom Fields." You can see it right here, and if it's not checked, you need to check it now.
"Custom Fields" usually appear below the editor, right down here. And within the "Custom Fields," you can create as many custom fields as you want. Now, many plugins already create custom fields, and in this course we're going to create a new plugin that creates a new custom field. But I'll briefly show you how it works before we do that. A custom field is just an additional field within my editor. I can add a new field here if I want to. And give it the name "views." Then I can add a value to that field. Let's say the number "10." And then when I update the post and scroll down, you'll see this "views" field with the value "10" is stored.
Meaning I can access it either from the back end like you see here, or I can display the number on the front end of the site if I want to. So what we're going to do in this course, is create a plugin that first of all creates this field "views," and then populates it with the number "0." And then every time a post is loaded, it goes back into this "views" field, and grabs the existing number, adds one to it and puts it back in. That way we're counting the number of views and associating that number directly with the individual post.
And then we can use this number to display the most popular posts in a widget or somewhere else on the site. So now that you know what we're going to do, let's dive into the code and build our own popular post function.