Selljam logo

FacetWP Filters: Convert Serialised Array to Check Boxes or Other Filters

This FacetWP tutorial shows how to work with data arrays. It's a great plugin for WordPress that allows you to filter custom fields data from your posts (and CPTs) into a neat and structured way.

FacetWP is a must-have plugin for WordPress which comes packed with powerful features that allow you to filter the custom fields data from your posts (and CPTs). This enables users to filter the results of a page to the criteria they select and allowing powerful search capabilities and a better user experience all round.

This is especially useful when used with WordPress plugins like Toolset or Advanced Custom Fields, where you want to display a string or value from custom fields as checkboxes, dropdowns or search boxes for the user to select as a filter to narrow the results.

But the problem comes in when various plugins store that data in different ways...

How do I display serialised data and arrays as a filter with FacetWP? While FacetWP displays custom field values from plugins such as Advanced Custom Fields which has built-in integration. Toolset is one such plugin that uses a serialised data array for its checkboxes (amongst other fields) which FacetWP does not support... At least not right out of the box, however the solution below allows FacetWP to read the serialised data array.

An example of this can be seen below where FacetWP is unable to index and display serialized data (in an array) which is created by a Toolset checkbox . Some plugins work with strings whereas others work with more complicated fields such as arrays. Some of which can cause issues.

The raw value of an array for a custom field would look like below on your database. Where "Wholesale" is the portion of it that's desired for the final frontend filter.

Example of a custom field value stored as an array in the wp_postmeta table:

a:1:{s:64:"wpcf-fields-checkboxes-option1";a:1{i:0;s:9:"Wholesale";}}

If you are using a WordPress plugin (like Toolset) that uses data arrays and you'd like FacetWP to be able to use that data; then further below is a custom code snippet which is needed for FacetWP to be able to index the data correctly and display as a filter. An example of these filters can be found on our wordpress plugins page on the left hand side under the "Filters" heading.

Create The Filter & Code Snippet

  • Create a new Facet filter (or use and existing one) and copy the name, like facetwp_supplier_type as it would appear in example below. Save changes.
  • Install and activate the Code Snippets plugin (this removes the need to add custom snippets to your theme’s functions.php file which can potentially cause issues or your website to crash).
  • Click Snippets → "Add new" from the side menu to create a new snippet. Enter a name for the snippet then Copy-paste below to the snippet code field

The PHP code snippet below is needed to adjust how the FacetWP reads the data when it is stored as an array.

<?php

function fwp_index_serialized_field( $params, $class ) {
    if ( 'service_type' == $params['facet_name'] ) {
        $cf = (array) $params['facet_value']; // already unserialized
        foreach ( $cf as $row ) {
            foreach ( $row as $val ) {
                $params['facet_value'] = $val;
                $params['facet_display_value'] = $val;
                $class->insert( $params );
            }
        }
        return false;
    }
    return $params;
}
add_filter( 'facetwp_index_row', 'fwp_index_serialized_field', 10, 2 );

// Code snippet provided by Matt, the FacetWP plugin developer on github.

Your snippet should look like below screenshot. Remember to replace "service_type" from above with your actual facet name as found on your Settings → FacetWP page.

For this example the replaced value can be seen highlighted below, yours will be different.

Save the snippet and check "Only run in administration area" (as the indexing happens on the backend).

Facetwp array indexing code
New code snippet created using the Code Snippet plugin.
  • Next, go to your Facet Filter's page and click Re-index so that the new data can be added to the index table.
  • Copy the FacetWP filter's shortcode and add it to the page (or template) where your filters will be displayed. Save and view the results.

Multiple Facets with arrays? If there is more than one facet with a serialised array then you'll need to add the || "or" boolean operator to the if statement for each facet, like below.

if ( 'facetwp_supplier_type' || 'facetwp_another_type' == $params['facet_name'] ) {

That's it, all done. Clear the website's cache (including all CSS, JS files etc). Open your website and visit the page where the filters are located then hit Ctrl+F5 to refresh. If all went well your new filter should be displaying.

Written by:
James Mew
Selljam Founder | Head of eCommerce

With 18 years of experience in the eCommerce industry, I have successfully launched and grown multiple e-commerce businesses, the 2nd one hit 7-figures in revenue within its first year. In 2022, I joined a European food technology equipment and IT service provider as their Head of E-commerce, overseeing 14 eshops across Europe and South Africa. Selljam is where I share all those ecommerce tips, tricks and hacks learned along the way that I hope will also help you on your journey to success. 

Related Posts

Exploring Subdomain vs Subdirectory vs CCTLDs: Choosing the Right Domain Structure for SEO Success

Discover the 13 key factors for choosing the right domain structure that will ensure SEO success. Learn the pros and cons of subdomains, subdirectories, and CCTLDs to choose the best option for your website's SEO and improve your website traffic.

Google’s Product Evolution: A Timeline Of App Name Changes

Google has been constantly evolving its product names over the years. Some of these changes have been minor, while others have been more significant. Let's explore these name changes and some of the reasons behind them.

Customer Identity Verification Tools: The Best Apps to Protect Your Ecommerce Store

When secure card payments aren't enough and you need an extra layer of protection, then customer ID verification may be just what your business needs to level up. Comply with regulations and also protect your ecommerce business or app from fraud and chargebacks with the best customer identity verification tools.

Holy Sheet! Give Google Sheets Superpowers with These Add-ons

Looking for ways to supercharge your Google Sheets? Check out these amazing add-ons that can help you bring in data from your business or ecommerce store, automate tasks, analyze data, and create stunning visualizations.

Guide to Email Marketing Campaigns: The Automated Growth Hack for Ecommerce Businesses

Learn how to create and implement email marketing funnels to automate your sales and grow your ecommerce business. This article show you all the best campaigns and sequences to run on your ecommerce store.

How to Get Your PageSpeed Score In the 90s

Boost SERP rankings, improve your website's performance and SEO with these tips on how to get your PageSpeed score in the 90s. Get more traffic to your website and convert visitors on your "money pages".

Email Automation Tools and Platforms: Case Studies and Success Stories in the E-commerce Space

Learn how to use email automation to grow your ecommerce business. Discover the best email automation tools and platforms, and see how other ecommerce businesses are using them to achieve success.

Domain Buying Secrets: The Things I learned From Buying a €3,000 domain

Discover the secrets of buying a valuable domain name. Learn how to identify the best domains and avoid common pitfalls to securing your dream domain.

PDP Ecommerce: Anatomy of the Perfect Product Page

Learn what makes a perfect product page (PDP). Discover the key elements of it, and see how to optimize your product pages for conversions.

How to Use ChatGPT & AI for E-commerce to Boost Your Business

Discover how to use ChatGPT and AI to boost your ecommerce business, with tips on generating creative content, optimizing your website, and improving customer service.

Dictation Apps and Speech-to-Text Are Your Secret Weapons For Increased Efficiency and Productivity

Boost your productivity with dictation apps and speech-to-text technology. Learn how to choose the right tool for your needs and use it to create content, send emails, and more.

AI Chatbots for CX: The Future of Customer Service for eCommerce Stores

Learn how AI chatbots can help you improve customer service and boost sales for your ecommerce store. This guide includes information on the benefits of AI chatbots and tips on implementing them in your store.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram