Home › Forums › WoodMart support forum › Extend search to custom fields
Extend search to custom fields
- This topic has 3 replies, 2 voices, and was last updated 4 years, 6 months ago by Artem Temos.
-
AuthorPosts
-
May 8, 2020 at 9:14 am #193382
CMParticipantHello,
We would like to extend the search capability to search within WP custom fields and we would please like some guidance on that.
We figured out that for starters we need to change some code inside
woodmart\inc\modules\search.php
and more specifically inside the functionwoodmart_sku_search_query()
edit the:$sku_to_id = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE ( meta_key='_writer_original' OR meta_key='_writer' OR meta_key='_barcode' OR meta_key='_isbn' OR meta_key='_sku') AND meta_value LIKE '%%%s%%';", wc_clean( $term ) ) );
Then we made sure to void the
version_compare
check so the new function woodmart_sku_search_query_new() is not used (cause this is looking at separate lookup table).Is there any other place we need to change code for the search to work in WP Custom Fields too ?
May 8, 2020 at 11:06 am #193416
Artem TemosKeymasterHello,
This function adds SKU functionality to the search only. But the main algorithm is built-in in WordPress and WooCommerce. There are no other places for the search queries in our theme.
Kind Regards
May 11, 2020 at 6:33 am #193982
CMParticipantThank you for your reply. Unfortunately either I didn’t understand it correctly or it isn’t accurate. I’m posting my finding here, hoping to help others.
We have found that there are 2 more files that need to be changed, for the search function to work correctly on custom fields. Those files are:
wp-content\themes\woodmart\inc\widgets\class-widget-layered-nav.php wp-content\themes\woodmart\inc\widgets\class-widget-price-filter.php
The changes to those files are the ones that I described in my first post.
Moreover, we needed to change this
$where = str_replace( ')))', ") OR ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . "))))", $where );
to this$where = str_replace( ')))', ")) OR ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")))", $where );
because some search results were partial.If someone wants to extend this template to be able to search within other custom fields (apart from sku), these are the changes that need to be made.
May 11, 2020 at 6:51 am #193986
Artem TemosKeymasterYes, these files are more related to filters widgets but should be adjusted as well. Thank you very much for posting your solution here.
-
AuthorPosts
Tagged: search
- You must be logged in to create new topics. Login / Register