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.

Suggested Topics

  • 2
  • 10
  • 2
  • 1
  • 3

Be the first to read the news!

We are always improving our products, adding new functions and fixes. Subscribe now to be the first to get the updates and stay informed about our sales! We are not spammy. Seriously.

Join Us Now!

We are a professional IT-team. Many of us have been working in a Web IT field for more than 10 years. Our advanced experience of software development has been employed in the creation of the WordPress FullText Search plugin. All solutions implemented into the plugin have been used for 5 or more years in over 60 different web-projects.

We are looking forward to your comments, requests and suggestions in relation to the current plugin and future updates.

ewm-logo-450

The forum powered by NodeBB | Contributors