Refers to FiboSearch Pro only

Adding non-standard data to the search index

FiboSearch is a powerful and highly customizable search solution for WooCommerce, enabling users to search for products by a variety of data such as descriptions, tags, categories, SKUs, and more. Its flexibility extends even further through the use of code snippets, allowing you to index and search additional data, including custom fields, alt attributes of images, vendor names, and more.

In addition to product-related data, FiboSearch also provides the ability to include non-standard data in the search index for posts, pages, custom post types, and taxonomies. This allows for a more tailored and comprehensive search experience across your entire site.

This document will guide you through the steps required to add non-standard data to the FiboSearch index, helping you take full advantage of the plugin’s customization capabilities.

Table of Contents

Search product variations by custom fields

The “Search in custom fields” option in FiboSearch settings applies only to simple and variable products. To make product variations searchable by custom fields as well, you can use the following code:

function fibosearch_get_product_variations_custom_fields_values( $product ) {
	$keys       = array( '_ts_gtin', '_another_custom_field' );
	$variations = $product->getAvailableVariations();

	if ( empty( $variations ) ) {
		return '';
	}

	$custom_fields_values = array();
	foreach ( $variations as $variation ) {
		foreach ( $keys as $key ) {
			$value = get_post_meta( $variation['variation_id'], $key, true );
			if ( ! empty( $value ) ) {
				$custom_fields_values[] = $value;
			}
		}
	}

	return join( ' | ', $custom_fields_values );
}

add_filter( 'dgwt/wcas/indexer/items_row', function ( $row ) {
	if ( isset( $row['post_type'] ) ) {
		return $row;
	}
	$product = new DgoraWcas\Product( $row['ID'] );
	if ( ! $product->isValid() || ! $product->isType( 'variable' ) ) {
		return $row;
	}
	$row['variations_custom_fields'] = fibosearch_get_product_variations_custom_fields_values( $product );

	return $row;
} );

add_filter( 'dgwt/wcas/tnt/indexer/searchable/product_data', function ( $document, $product_id, $product ) {
	$document['variations_custom_fields'] = fibosearch_get_product_variations_custom_fields_values( $product );
	return $document;
}, 10, 3 );

Learn how to add this snippet to your WordPress.

Replace _ts_gtin and _another_custom_field with your custom fields.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search products by alt attributes of images

Here’s how to make the products searchable by the HTML alt attributes of the featured image and product gallery images:

add_filter( 'dgwt/wcas/tnt/source_query/data', function ( $data, $sourceQuery, $onlyIDs ) {
	if ( empty( $data ) || ! is_array( $data ) ) {
		return $data;
	}

	if ( $onlyIDs ) {
		return $data;
	}

	foreach ( $data as $index => $row ) {
		$product   = wc_get_product( $row['ID'] );
		$image_ids = [];

		if ( ! empty( $product->get_image_id() ) ) {
			$image_ids[] = $product->get_image_id();
		}

		$gallery_ids = $product->get_gallery_image_ids();
		if ( ! empty( $gallery_ids ) ) {
			$image_ids = array_merge( $image_ids, $gallery_ids );
		}

		$data[ $index ]['image_alts'] = '';
		foreach ( $image_ids as $image ) {
			$alt = get_post_meta( $image, '_wp_attachment_image_alt', true );
			if ( ! empty( $alt ) ) {
				$data[ $index ]['image_alts'] .= ' ' . $alt;
			}
		}
	}

	return $data;
}, 15, 3 );

Learn how to add this snippet to your WordPress.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search products by Dokan store name

If you’re using the Dokan multi-vendor marketplace plugin, you can make products searchable by the Dokan store name with the following code:

function fibosearch_index_dokan_data( $document, $product_id ) {
	$author_id  = get_post_field( 'post_author', $product_id );
	$store_info = get_user_meta( $author_id, 'dokan_store_name', true);

	if ( ! empty($store_info) ) {
		$document['custom_dokan_data'] = $store_info;
	}
	return $document;
}

add_filter( 'dgwt/wcas/tnt/source_query/data', function ( $data, $sourceQuery, $onlyIDs ) {
	if ( empty( $data ) || ! is_array( $data ) ) {
		return $data;
	}

	if ( $onlyIDs ) {
		return $data;
	}

	foreach ( $data as $index => $row ) {
		$data[ $index ] = fibosearch_index_dokan_data( $row, $row['ID'] );
	}

	return $data;
}, 10, 3 );

add_filter( 'dgwt/wcas/tnt/indexer/searchable/product_data', function ( $document, $productID, $product ) {
	return fibosearch_index_dokan_data( $document, $productID );
}, 10, 3 );

Learn how to add this snippet to your WordPress.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search taxonomies by description

You can make taxonomies (such as categories, tags, and custom taxonomies) searchable by their description using the following code:

add_filter( 'dgwt/wcas/indexer/taxonomy/document_data', function ( $documentData, $term ) {
	$documentData['description'] = $term->description;
	return $documentData;
}, 10, 2 );

Learn how to add this snippet to your WordPress.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search product categories by ACF custom fields

If you want to make product categories searchable by custom fields added to them through the Advanced Custom Fields (ACF) plugin, use the following code:

add_filter( 'dgwt/wcas/indexer/taxonomy/document_data', function( $documentData ) {
	$custom_values = fibosearch_get_cat_custom_fields( $documentData['ID'] );
	$documentData['custom_fields'] = implode( ' | ', $custom_values );
	return $documentData;
} );

function fibosearch_get_cat_custom_fields( $cat_id ) {
	$custom_fields = array( 'field_65c90bd6318e8', 'field_65baa11bc8ca3' ); // category custom fields
	$custom_values = array();
	foreach ( $custom_fields as $custom_field ) {
		$meta = get_term_meta( $cat_id, $custom_field, true );
		if ( $meta ) {
			$custom_values[] = $meta;
		}
	}
	return $custom_values;
}

Learn how to add this snippet to your WordPress.

Replace field_65c90bd6318e8 and field_65baa11bc8ca3 with your product category custom fields.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search posts, pages and custom post types by description

By default, FiboSearch only searches in the titles of posts, pages, and custom post types. To include their descriptions in the search index, use the following code:

add_filter( 'dgwt/wcas/tnt/post_source_query/description', '__return_true' );

Learn how to add this snippet to your WordPress.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.

Search posts by tags

To make posts searchable by their tags, use the following code snippet:

function dgwt_wcas_index_post_tags( $document, $post_id ) {
	$tags = wp_get_post_tags($post_id);

	foreach ($tags as $tag) {
		$document[] = $tag->name;
	}

	return $document;
}

add_filter( 'dgwt/wcas/tnt/post_source_query/data', function ( $data, $sourceQuery, $onlyIDs ) {
	if ( empty( $data ) || ! is_array( $data ) ) {
		return $data;
	}

	if ( $onlyIDs ) {
		return $data;
	}

	foreach ( $data as $index => $row ) {
		$data[ $index ] = dgwt_wcas_index_post_tags( $row, $row['ID'] );
	}

	return $data;
}, 10, 3 );

Learn how to add this snippet to your WordPress.

After adding the code, go to WooCommerceFiboSearchIndexer (tab) and rebuild the index.