How can I index WordPress category names with posts?


#1

I tried searching for a post by its category name, and it didn’t come up. How can I fix this?

At this time, the Swiftype WordPress plug-in doesn’t index category name values with posts. The default behavior is to index the category # defined by your WordPress instance.

However, this is something you can achieve by customizing your theme. First, make sure that your search engine is unlocked if it isn’t already (click the lock icon if you see it). Then add the following code to your functions.php file:

function swiftype_category_name_callback( $category ) {
    return $category->cat_name;
}

function update_swiftype_document_url( $document, $post ) {
    $categories = get_the_category( $post->ID );

    if ( $categories ) {
        $document['fields'][] = array( 'name' => 'category_name',
                                       'type' => 'string',
                                       'value' => array_map( "swiftype_category_name_callback", $categories ) );
    }

    return $document;
}

add_filter( 'swiftype_document_builder', 'update_swiftype_document_url' );

The above code example should do the trick in most cases, but it might need to be customized for your specific WordPress installation.

Once the custom function is in place, click the Synchronize with Swiftype button in the Swiftype tab of the WordPress admin dashboard to index the category_name values.