This Documentation is for SearchWP Version 3

Search WooCommerce Downloadable Product Document Content

Note: This documentation is for SearchWP 3.x
SearchWP 4: Search WooCommerce Downloadable (PDF) Product Document Content

If you’re selling document downloads in your WooCommerce store, it’s possible to have SearchWP index the content of those documents to make it searchable for your customers!

Note: this (like all document processing in SearchWP) requires that the WooCommerce Downloadable Product Downloads have been uploaded to the local WordPress Media library.

Index WooCommerce Document Content

We can take advantage of SearchWP’s ability to parse and make searchable the content from readable documents stored in the WordPress Media library. This can be paired with SearchWP’s powerful ability to index ‘extra’ metadata in its own index, without affecting the data stored in WordPress’ post or postmeta table.

The hook to make that happen is searchwp_extra_metadata and it’s really quite handy.

If you’d like to make your WooCommerce Downloadable document content searchable, you can add this snippet to your theme’s functions.php

// Tell SearchWP to parse WooCommerce Downloadable Product downloads for document content.
// The content will be extracted from downloadable documents where possible
// and stored as extra metadata with a key of `swp_wc_doc_content`
add_filter( 'searchwp_extra_metadata', function( $extra_metadata, $the_post ) {
if ( 'product' !== get_post_type( $the_post ) || ! class_exists( 'WC_Product' ) ) {
return $extra_metadata;
$product = new WC_Product( $the_post );
$downloads = $product->get_downloads();
if ( empty( $downloads ) ) {
return $extra_metadata;
// WooCommerce only stores a hashed ID, the filename, and the URL to the file
// but we need to retrieve the Media library ID for each downloadble file.
$upload_dir = wp_upload_dir();
foreach ( $downloads as $key => $download ) {
$relative_file_location = str_replace( trailingslashit( $upload_dir['baseurl'] ), '', $download['data']['file'] );
// We can use the relative file location to retrieve the post ID we need to parse the PDFs.
$file_id = get_posts( array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
'key' => '_wp_attached_file',
'value' => $relative_file_location,
) );
if ( empty( $file_id ) ) {
// Use SearchWP's parser to extract document content.
$parser = new SearchWPDocumentParser( $file_id[0] );
$product_content = ! empty( $parser->stored_content ) ? $parser->stored_content : $parser->extract_document_content();
// Store the extracted content as extra metadata.
$extra_metadata['swp_wc_doc_content'][] = $product_content;
return $extra_metadata;
}, 10, 2 );
// Add a Custom Field key for our WooCommerce Downloadable Product extracted Document Content.
add_filter( 'searchwp_custom_field_keys', function( $keys ) {
$keys[] = 'swp_wc_doc_content';
return $keys;
} );
view raw functions.php hosted with ❤ by GitHub

Once that’s done you’ll need to edit our engine configuration to include the custom meta key we’re working with: swp_wc_doc_content

Screenshot of WooCommerce document content added to SearchWP

Once our custom meta key that contains all of our WooCommerce Downloadable document content has been added to the search engine, you can save the engine and you’ll be prompted to rebuild the index, which is required.

Screenshot of SearchWP indicating the index needs to be rebuilt

As each WooCommerce Product is indexed, the snippet above will be checking for downloads, and when it finds them it will utilize SearchWP’s document parser to extract the content from those downloads and add it to the swp_wc_doc_content extra meta key.

Once the index is rebuilt, your customers will be able to search the content of your WooCommerce Downloadable products prior to having purchased them!