Get WPFTS Pro today with 25% discount!

Adding woocommerce SKU and taxonomy

  • Will this work?

    add_filter('wpfts_index_post', function($index, $post) {
    	global $wpdb;
    	// Basic tokens
    	 * This piece of code was commented out intentionally to display things
    	 * which was already done before in the caller code
    	$index['post_title'] = $post->post_title;
    	$index['post_content'] = strip_tags($post->post_content);
    	if ($post->post_type == 'product') {
    		$prefix = $wpdb->prefix;
                    //easy way to get all taxonomy even from other modules
    		$sql = " 
    			SELECT * FROM {$prefix}term_relationships AS tr
    			LEFT JOIN {$prefix}term_taxonomy AS tt ON ( tt.term_taxonomy_id = tr.term_taxonomy_id )
    			LEFT JOIN {$prefix}terms AS t ON ( t.term_id = tt.term_id )
    			WHERE tr.object_id = {$post->ID}
    		$taxonomy = $wpdb->get_results($sql,ARRAY_A);
    		foreach($taxonomy as $t){
    			$p[$t['taxonomy']] =$t['name'];
    		$data = array();
    		$data[] = get_post_meta($post->ID, '_sku', true);
                    //for parts with diff SKU with diff vendor stored in custom fields
    		$data[] = get_post_meta($post->ID, 'MPN Number', true);
    		$data[] = get_post_meta($post->ID, 'MPN2 Number', true);
    		$index['sku'] = implode(' ', $data);
    		$index['product_taxonomy'] = implode(' ', $taxonomy);
    	return $index;
    }, 3, 2);

  • Yes, it looks correct. I think there should be a better way to get taxonomy (using WP native methods). Also, I see a bit strange names for custom post meta names (MPN Number and MPN2 Number) - please be sure they are stored in a database like this exactly.

    You can check your hook using Sandbox / Test Index. Just put the product post ID into the input and press the button. It should show you all cluster values, including 'sku' and 'product taxonomy'. Next, you can go to Edit one product and press Update to force WPFTS to rebuild index for this product. After that, this product should become searchable by sku and taxonomy.

    If so, you can press Rebuild Index to rebuild an index for all products.