→ How to Index Serialized Data

With FacetWP, it’s possible to index custom fields stored as a serialized array.

Let’s say you have a facet named days_of_week that uses a custom field with the same name. In the database, an example postmeta row looks like:

a:2:{i:0;s:6:"Monday";i:1;s:8:"Thursday";}

To index it, add the following code to functions.php, then hit the Re-index button.

<?php

function index_serialized_data( $params, $class ) {
    if ( 'days_of_week' == $params['facet_name'] ) {
        $values = (array) $params['facet_value'];
        foreach ( $values as $val ) {
            $params['facet_value'] = $val;
            $params['facet_display_value'] = $val;
            $class->insert( $params );
        }
        return false; // skip default indexing
    }
    return $params;
}
add_filter( 'facetwp_index_row', 'index_serialized_data', 10, 2 );