Display the Most Viewed Posts - P2 - using Meta Box and Oxygen
Tags: tutorials, customfields, dynamicdata, oxygen
Published: 2022-12-20 02:25:31

Displaying the most viewed posts on your business website is a convenient way to attract viewers and potential customers. In this article, we will show you how to display your popular posts by views using <strong>Meta Box</strong> and <strong>Oxygen</strong>.

<!--more-->

The image below is a specific example:

<h2><img class="aligncenter" src="https://i.imgur.com/4kOQh7l.png" alt="Example of displaying the most viewed posts" width="1000" height="507" />Video Version</h2>

https://youtu.be/3P6KJHquRL4

<h2>Before Getting Started</h2>

Since we're going to display the most viewed posts, we'll use a third-party plugin to count the post views. It's the <a href="https://wordpress.org/plugins/post-views-counter/">Post Views Counter</a> plugin.

For the posts, we'll create a <strong>custom post type</strong> named <strong>Restaurants</strong>, and each restaurant will be a post of that post type.

When displaying the most viewed posts, you may wish to show more than just the title and featured image (the default fields). In this case, I'll display extra information about the address, voucher, and logo. They'll be saved in the custom fields of the post.

Here are the tools we need for this practice:

<ul>

   <li><a href="https://wordpress.org/plugins/meta-box/">Meta Box core plugin</a> to have a framework for creating a custom post type and custom fields;</li>

   <li><a href="https://metabox.io/plugins/custom-post-type/">MB Custom Post Type</a> : to create custom post types;</li>

   <li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a> : to have a UI on the back end to create custom fields easily;</li>

   <li><a href="https://metabox.io/plugins/mb-views/">MB Views</a>: help us create a template for the most viewed posts section.</li>

</ul>

Finally, we need <a href="https://oxygenbuilder.com/">Oxygen Builder</a>. You should use its 3.9 version or higher, which has native integration with Meta Box.

<h2>Create a New Custom Post Type</h2>

Go to <strong>Meta Box &gt; Post Types</strong> to create a new post type for the restaurants.

<img class="aligncenter" src="https://i.imgur.com/MSAfcUL.png" alt="Create a new custom post type" width="1001" height="708" />

<h2>Create Custom Fields</h2>

Each restaurant may have extra information saved in custom fields, so go to <strong>Meta Box &gt; Custom Fields</strong> and create them.

<img class="aligncenter" src="https://i.imgur.com/JqG6idp.png" alt="Create custom fields" width="1000" height="636" />

After creating all the needed fields, go to the <strong>Settings</strong> tab &gt; <strong>Location</strong> &gt; choose <strong>Post Type</strong> as the <strong>Restaurant</strong> post type we've just created to apply the custom fields.

<img class="aligncenter" src="https://i.imgur.com/XGY0Ch6.png" alt="Set location for the created custom fields" width="1000" height="636" />

In the post editor, you will see all of the newly created custom fields appear.

<img class="aligncenter" src="https://i.imgur.com/0tzGv05.png" alt="All created custom fields appear in the post editor" width="1008" height="831" />

<h2>Count the Posts View</h2>

Normally, there is no information about the posts' view available in WordPress by default. We must do it on our own. Install the <strong>Post Views Counter</strong> plugin, then go to <strong>Settings &gt; Post Views Counter</strong> and check the box next to the post type whose views you want to count.

<img class="aligncenter" src="https://i.imgur.com/U9NGVTt.png" alt="Install Post views counter plugin" width="1000" height="783" />

Right after that, you can see the view numbers of each restaurant appear. Based on these numbers, we'll choose which restaurant has the most views and display it in the section.

<img class="aligncenter" src="https://i.imgur.com/y4mB0Lj.png" alt="Choose which restaurants have the most views to display in the section" width="1000" height="833" />

<h2>Create the Section</h2>

Edit a page with <strong>Oxygen</strong>, e.g., the homepage.

First, choose the <strong>Section</strong> component to contain all of the restaurant information. Then, add a <strong>Heading</strong> element and enter the title.

<img class="aligncenter" src="https://i.imgur.com/iTyWClu.png" alt="Add a Heading element to enter the title" width="1000" height="624" />

<h3>Set a Condition</h3>

To get all the posts of the <strong>Restaurant</strong> post type, select the <strong>Repeater</strong> component. Then, go to the <strong>Query</strong> section to choose the data source and set conditions.

Normally, you can choose the <strong>default</strong> or <strong>custom</strong> type to get the posts. However, in this case, I only want a specific number of posts to be displayed based on the condition, so I chose the <strong>advanced</strong> query type.

<img class="aligncenter" src="https://i.imgur.com/EdGsVYQ.png" alt="Choose Advanced query type to display specific number of posts" width="1000" height="500" />

Now, let's create conditions to choose which posts to display. We'll add some query parameters as follows:

First, add a <code>post_type</code> parameter to specify that we'll get only posts from the restaurant post type. In the box, as shown in the image below, enter the ID of the post type for which you want to get the data.

<img class="aligncenter" src="https://i.imgur.com/M1RluBt.png" alt="Add a post_type parameter " width="1000" height="628" />

Then, add another parameter named <code>meta_query</code>. This aims to choose which posts in the restaurant post type are displayed.

<img class="aligncenter" src="https://i.imgur.com/OP6iU1a.png" alt="Add a meta_query parameter" width="1250" height="775" />

To limit the number of posts to display in the section, we add the <code>post_per_page</code> parameter and set the number of posts you want to display.

<img class="aligncenter" src="https://i.imgur.com/kuZVzor.png" alt="Add the post_per_page parameter to limit the number of posts to display" width="1000" height="864" />

We've completed the condition settings. After applying, there are 6 boxes for 6 posts. It's equivalent to the number of posts we set for the section.

<img class="aligncenter" src="https://i.imgur.com/AAieadn.png" alt="There is a box for each post" width="1000" height="564" />

<h3>Display the Restaurants' Information</h3>

Now, it's time to display information about each restaurant. In the div that is inside the <strong>Repeater</strong> component, add an <strong>Image</strong> component to display the restaurant's image, then connect it with the post's featured image.

<img class="aligncenter" src="https://i.imgur.com/6jmB9MG.png" alt="Add an Image component in the div that is inside the Repeater component" width="1000" height="564" />

For the voucher information, choose a <strong>Text</strong> component and connect it to the corresponding field.

<img class="aligncenter" src="https://i.imgur.com/t6dXKk0.gif" alt="Choose a Text component for the voucher information" width="1000" height="562" />

The next piece of restaurant information we want to display is the restaurant's logo. Choose an <strong>Image</strong> component. Because the logo field is created by Meta Box and it returns the <strong>URL</strong>, go to the <strong>Image URL</strong> tab to get data from this field.

<img class="aligncenter" src="https://i.imgur.com/9WELhe2.gif" alt="Choose an Image component for the restaurant's logo" width="1000" height="562" />

To get the restaurant's name automatically, add the <strong>Text</strong> component and connect it with the post's title.

<img class="aligncenter" src="https://i.imgur.com/3LmuR7n.png" alt="Add the text component to get the restaurant's name" width="1250" height="930" />

For the address information, do the same with the <strong>Voucher</strong> field. Add a <strong>Text</strong> component, then connect it to the <strong>Address</strong> field.

<img class="aligncenter" src="https://i.imgur.com/TQfAjAv.png" alt="Add a text component " width="1250" height="880" />

That's all. Now, the information for all of the most viewed restaurants is now available on the homepage.

<img class="aligncenter" src="https://i.imgur.com/YOfw77V.png" alt="All the information of the most viewed restaurants is available" width="1000" height="886" />

<h2>Style the Section</h2>

For styling, go back to the page editor with <strong>Oxygen</strong>. Then, choose each component and change the settings to style them.

You can also add some CSS in the <strong>Custom CSS</strong> tab to have a more advanced style.

<img class="aligncenter" src="https://i.imgur.com/QfE6Gyt.png" alt="Add some CSS code" width="1000" height="800" />

Go to the homepage; all the featured restaurants are displayed as we want.

<img class="aligncenter" src="https://i.imgur.com/88pjwUb.png" alt="The final result " width="1000" height="896" />

<h2>Final Words</h2>

Through this article, I hope you'll be able to create your own version of the most-viewed restaurants with the help of<strong> Meta Box</strong> and <strong>Oxygen</strong>. Just give it a try and provide your findings in the comment section. Best of luck!