Home Forums WoodMart support forum Extend search to custom fields

Extend search to custom fields

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #193382

    CM
    Participant

    Hello,

    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 function woodmart_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 ?

    #193416

    Artem Temos
    Keymaster

    Hello,

    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

    #193982

    CM
    Participant

    Thank 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.

    #193986

    Artem Temos
    Keymaster

    Yes, these files are more related to filters widgets but should be adjusted as well. Thank you very much for posting your solution here.

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)