Can the Swiftype WordPress plugin index custom post metadata?


#1

We have some custom fields defined in for our posts using post metadata. Can we index this with Swiftype?

As of this writing, the Swiftype WordPress plugin doesn’t index custom fields by default, but you can add it to your index using the swiftype_document_builder filter.This filter is called after building a document from a post for indexing in Swiftype. It takes two arguments, the document and the post and returns the document. Inside the function, you can add fields to the document for indexing.

For example, if you had a custom field called price, you could index it as an integer field like this:

function update_swiftype_document_url( $document, $post ) {
    $document['fields'][] = array( 'name' => 'price',
                                   'type' => 'integer',
                                   'value' => get_post_meta( $post->ID, 'price', true ));

    return $document;
}

add_filter( 'swiftype_document_builder', 'update_swiftype_document_url', 10, 2 );

Using the swiftype_document_builder filter you can customize your search engine schema to match your needs. Typically, when indexing custom fields, you’ll want to adjust your queries as well, which you can do with the swiftype_search_params filter.

For more details, be sure to read our Search Engine Schema Design tutorial,Customizing Results with the Swiftype WordPress Search Plugin tutorial and our API documentation about indexing and searching.