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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login