How to Filter Posts by Custom Fields - P2 - using Meta Box and FacetWP
Tags: tutorial, filter, dynamicdata
Published: 2023-01-10 02:21:03

On an archive page, we'll display posts and filter them by the values stored in the custom fields created by Meta Box. With the help of <strong>FacetWP</strong>, we can do it easily without coding.

<!--more-->

Here is an archive that I created for example.

<img class="aligncenter" src="https://i.imgur.com/KcIh5xL.gif" alt="The example of filtering posts" width="1000" height="562" />

<h2>Video Version</h2>

https://youtu.be/kjkVaC_QKQs

<h2>Before Getting Started</h2>

On the page, we'll have 2 sections. One is used to filter the restaurants by conditions. We'll create this part using <strong>FacetWP</strong>. The second part is to show up all the restaurants.

<img class="aligncenter" src="https://i.imgur.com/1KwGTbg.png" alt="Two sections of the page" width="1000" height="625" />

All the information of the restaurants is saved in a custom post type. Each restaurant is a post of that post type. Besides the basic information such as name and image of the restaurants which are the post title and featured images, other additional information will be saved in different custom fields.

In the upcoming execution, we'll create them one by one. No matter if you're using any page builder or not, you can also use <strong>FacetWP</strong> to filter posts as I'm doing in this practice. In this case, I chose <strong>Elementor</strong> to build the page and use the pro version.

For the custom fields, we need<a href="https://wordpress.org/plugins/meta-box/"> Meta Box core plugin</a> and some of its extensions, including:

<ul>

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

   <li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a>: to have an intuitive UI to create custom fields in the backend;</li>

   <li><a href="https://metabox.io/plugins/meta-box-facetwp-integrator/">Meta Box - FacetWP Integrator</a>: to create a filter based on the custom fields;</li>

   <li><a href="https://metabox.io/plugins/mb-elementor-integrator/">Meta Box Elementor Integrator</a>: to connect and display custom fields created by Meta Box in the <strong>Elementor</strong>'s dynamic tags.</li>

</ul>

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

Go to <strong>Meta Box &gt; Post Types &gt; Add New</strong> to create a new post type.

<img class="aligncenter" src="https://i.imgur.com/6sOZGUC.png" alt="Create a custom post type" width="1000" height="630" />

After publishing, we'll have a new <strong>Restaurants</strong> menu in the admin dashboard.

<img class="aligncenter" src="https://i.imgur.com/TnHSOKQ.png" alt="The created post type appears in the admin dashboard" width="1000" height="779" />

Then, let's create custom fields to save restaurants' information in the next step.

<h2>Create Custom Fields</h2>

Go to <strong>Meta Box &gt; Custom Fields</strong> to create a new field group. Here are the field that I created:

<img class="aligncenter" src="https://i.imgur.com/z2NgC9J.png" alt="The created custom fields" width="1000" height="852" />

For the <strong>Voucher</strong> information, choose the <strong>Checkbox list</strong> field to allow adding some options in the <strong>Choices</strong> box. These options will be used as a condition to filter posts when creating a filter section in the next step.

<img class="aligncenter" src="https://i.imgur.com/CAFAseK.png" alt="Choose the Checkbox list field for the Voucher information to add some options in the Choices box" width="1000" height="628" />

After creating all the needed fields, move to the <strong>Settings</strong> tab, choose <strong>Location</strong> as <strong>Post Type</strong> and select <strong>Restaurant</strong> to apply these fields to this post type.

<img class="aligncenter" src="https://i.imgur.com/aH4gsoe.png" alt="Set Location for the created post type" width="1000" height="363" />

When creating a new post in Restaurant post type, you'll see all the created custom fields:

<img class="aligncenter" src="https://i.imgur.com/kNNgVlz.png" alt="All created custom fields appear in a new post of the created post type" width="1000" height="500" />

<h2>Create a Template</h2>

Depending on the page builder you're using, there will be some differences in the creating page and template. You may need to create a template for the page or edit the page directly.

In this case, with <strong>Elementor</strong>, we need to create one. This template is to display all the posts along with their information. About the filter section, we'll create it later.

Since we'll have a list of restaurants on the page, we need a special type of template which is a loop. This option is provided by <strong>Elementor Custom Skin</strong>. You can try it.

Go to <strong>Templates</strong> &gt; <strong>Theme Builder</strong> and choose <strong>Loop.</strong>

<img class="aligncenter" src="https://i.imgur.com/j1X9403.png" alt="Go to Templates and then Theme Builder " width="1000" height="471" />

<img class="aligncenter" src="https://i.imgur.com/C3XZx7u.png" alt="Choose Loop option" width="1000" height="298" />

Remember to set the preview for the template.

Since the image and name of the restaurants are the featured image and title of the post, add the <strong>Featured Image</strong> and <strong>Post Title</strong> elements.

<img class="aligncenter" src="https://i.imgur.com/t88N4jD.png" alt="Add the Featured Image and Post Title elements " width="1000" height="481" />

For the address of the restaurant, add the <strong>Text Editor</strong> element. Since the restaurants' addresses are saved in a custom field created by Meta Box and the custom field is for post, let's use the <strong>Dynamic Tags</strong> &gt; choose <strong>Meta Box Fields</strong> in the <strong>Post</strong> section.

<img class="aligncenter" src="https://i.imgur.com/dI5pXSb.png" alt="Add the Text Editor element for the address of the restaurant" width="1000" height="517" />

And then, select the corresponding field. In this case, it's <strong>Address</strong>.

<img class="aligncenter" src="https://i.imgur.com/tG9sWJw.png" alt="Connect the Text Editor element to the correct field by using Dynamic Tags" width="1000" height="518" />

For the voucher information, do likewise.

<img class="aligncenter" src="https://i.imgur.com/NRH3aIC.png" alt="Do likewise with the voucher information" width="1000" height="538" />

<img class="aligncenter" src="https://i.imgur.com/D9iY0Xr.png" alt="Connect the element to the right field" width="1000" height="552" />

Now, all the restaurant's information has been displayed.

<img class="aligncenter" src="https://i.imgur.com/PQzv5RG.png" alt="All the information has been obtained" width="1000" height="677" />

Save the template and remember to set the condition to apply this template.

<img class="aligncenter" src="https://i.imgur.com/23Wa2Fh.png" alt="Save the template and remember to set the condition to apply this template" width="1000" height="639" />

<h2>Create the Page</h2>

Go to <strong>Pages</strong> to create a new page and <strong>edit it with Elementor.</strong>

<img class="aligncenter" src="https://i.imgur.com/jogEKj6.png" alt="Create a new page" width="1000" height="373" />

Add the <strong>Posts</strong> element to the page.

<img class="aligncenter" src="https://i.imgur.com/VGioxUT.png" alt="Add the Posts element to the page" width="1000" height="599" />

In the <strong>Skin</strong> setting of the <strong>Posts</strong> element, choose <strong>Custom</strong> and then select the default template as the one we've just created.

<img class="aligncenter" src="https://i.imgur.com/YwgDCzr.png" alt="In the Skin setting of the Posts element, choose Custom and then select the created template " width="1006" height="568" />

Next, go to the <strong>Query</strong> section, choose the source as the post type you want. In this case, it's <strong>Restaurant</strong>.

<img class="aligncenter" src="https://i.imgur.com/ef865A8.png" alt="In the Query section, choose the source as the wanted post type" width="1005" height="523" />

Now, the preview shows all the posts with the information as we want.

<img class="aligncenter" src="https://i.imgur.com/UIxRMnQ.png" alt="The preview shows all the posts with the information." width="1000" height="477" />

Let's see how to add a filter section in the next step.

<h2>Add Filter</h2>

This task will be done by <strong>FacetWP</strong>. If you use other page builders, you can also create the filter as I'm doing in this practice.

In the admin dashboard, go to <strong>Settings</strong> and choose <strong>FacetWP</strong> to create a new filter.

<img class="aligncenter" src="https://i.imgur.com/xR9nNT6.png" alt="Create a new filter" width="1000" height="732" />

Since we will filter the post by the promotion of the restaurant which is saved in the <strong>Voucher</strong> field, choose the <strong>Data Source</strong> as <strong>Voucher</strong> in the <strong>Restaurant</strong> post type.

<img class="aligncenter" src="https://i.imgur.com/TBkcCS5.png" alt="Choose the Data Source as Voucher in the Restaurant post type" width="1000" height="404" />

Save the changes and copy the shortcode of the facet.

<img class="aligncenter" src="https://i.imgur.com/7KTGmPH.png" alt="Save the changes and copy the shortcode of the facet" width="1000" height="393" />

Then, go back to the created page editor with <strong>Elementor</strong>, add a <strong>Shortcode</strong> element to paste the code we've just copied.

<img class="aligncenter" src="https://i.imgur.com/jjFu27A.png" alt="Add a Shortcode element to paste the copied code" width="1000" height="525" />

After applying the code, you'll see a section to filter posts in the frontend. All the restaurants having the same coupons are filtered correctly.

<img class="aligncenter" src="https://i.imgur.com/ZUipHsz.gif" alt="All the restaurants having the same coupons are filtered correctly" width="1000" height="562" />

<h2>Style the Page</h2>

Go back to the template of the page, you can style each element as you want.

In the event that you want to have more further styling, you can use some CSS code.

<img class="aligncenter" src="https://i.imgur.com/6k2QCRy.png" alt="Style the page" width="1000" height="772" />

All the changes in style will be applied immediately. This is how it looks on the frontend. The filter section created by <strong>FacetWP</strong> works well and all the posts have been filtered.

<img class="aligncenter" src="https://i.imgur.com/KcIh5xL.gif" alt="The final result" width="1000" height="562" />

<h2>Last Words</h2>

Using custom fields with the help of <strong>FacetWP</strong> is a brilliant way to create conditions and filter posts. If you see this practice is helpful, try it out and share the result with us. If you want to suggest any ideas, feel free to let us know by leaving a comment. Good luck!