Note: You are viewing the documentation for Version 4View Version 3 Docs

Indexing and Searching WooCommerce Product Variation SKUs

Using a couple of SearchWP’s hooks you can easily incorporate your Variable Product SKUs into SearchWP searches.

WooCommerce stores Product Variations as separate posts, so the first step will be to tell SearchWP to store some custom metadata for the parent product (the one that contains all of the Variations with SKUs).

Another optional hook will add this ‘extra’ Custom Field name to the dropdown when configuring your Product Custom Fields as you set up your Engine.

All hooks should be added to your custom SearchWP Customizations Plugin.

// @link
// Index WooCommerce Product Variation SKUs alongside the parent Product.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
$entry = $entry->native();
if ( ! $entry instanceof \WP_Post || 'product' !== get_post_type( $entry ) ) {
return $data;
// The 'extra' meta_key we'll use to store these SKUs.
$product_variation_key = 'swp_wc_product_variation_sku';
// Retrieve all Product Variations.
$product_variations = get_posts( [
'post_type' => 'product_variation',
'nopaging' => true,
'fields' => 'ids',
'post_parent' => $entry->ID,
] );
if ( empty( $product_variations ) ) {
return $data;
$product_variation_skus = [];
foreach ( $product_variations as $product_variation ) {
$product_variation_skus[] = get_post_meta( $product_variation, '_sku', true );
$data['meta'][ $product_variation_key ] = new \SearchWP\Tokens( array_filter( $product_variation_skus ) );
return $data;
}, 20, 2 );
// Add 'extra' meta_key as available option.
add_filter( 'searchwp\source\attribute\options', function( $keys, $args ) {
if ( $args['attribute'] !== 'meta' ) {
return $keys;
// This key is the same as the one used to store the Product Variation SKUs
// in the searchwp\entry\data hook above, they must be the same.
$product_variation_key = 'swp_wc_product_variation_sku';
// Add "Product Variation SKUs" Option if it does not exist already.
if ( ! in_array(
array_map( function( $option ) { return $option->get_value(); }, $keys )
) ) {
$keys[] = new \SearchWP\Option( $product_variation_key, 'Product Variation SKUs' );
return $keys;
}, 20, 2 );

With those hooks added to your SearchWP Customizations plugin, you’ll see the ‘extra’ Custom Field show up when you search for sku

Screenshot of Product Variation SKU Custom Field

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP