Leon Chan
Leon Chan

Reputation: 1

Divi search module cant search woocommerce product there are no result in result page

my website is https://trendyselect.net/ when I want to search woocommerce product its always display result not found

I added code snippet in function.php

with following code but nothing work

/* Search Product Tags */
add_filter('posts_search', 'wpz_add_product_tags_to_search', 10, 2);
function wpz_add_product_tags_to_search($searchSql, $query = false) {
if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
return $searchSql;
}
    if ($searchSql && isset( $_GET['et_pb_searchform_submit'] )) {
        global $wpdb;
        $searchSql = preg_replace(
            '/ (AND|OR) \\('.preg_quote($wpdb->posts).'\\.post_content (NOT )?LIKE \'(.+)\'\\)/U',
            '$0 $1 $2 EXISTS( SELECT 1 FROM '.$wpdb->term_relationships.' JOIN '.$wpdb->term_taxonomy.' USING (term_taxonomy_id) JOIN '.$wpdb->terms.' USING (term_id) WHERE object_id='.$wpdb->posts.'.ID AND taxonomy="product_tag" AND name LIKE \'$3\')',
            $searchSql
        );
    }
    return $searchSql;
}
add_action( 'wp_loaded', 'wpz_remove_default_search' );
function wpz_remove_default_search() {
    remove_action( 'pre_get_posts', 'et_pb_custom_search' );
    add_action( 'pre_get_posts', 'wpz_custom_search' );
}
function wpz_custom_search( $query = false ) {
    if ( is_admin() || ! is_a( $query, 'WP_Query' ) || ! $query->is_search ) {
        return;
    }
    if ( isset( $_GET['et_pb_searchform_submit'] ) ) {
        $postTypes = array();
        if ( ! isset($_GET['et_pb_include_posts'] ) && ! isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'post' );
        }
        if ( isset( $_GET['et_pb_include_pages'] ) ) {
            $postTypes = array( 'page' );
        }
        if ( isset( $_GET['et_pb_include_posts'] ) ) {
            $postTypes[] = 'post';
        }
        /* BEGIN Add custom post types */
        $postTypes[] = 'product';
        /* END Add custom post types */
        $query->set( 'post_type', $postTypes );
        if ( ! empty( $_GET['et_pb_search_cat'] ) ) {
            $categories_array = explode( ',', $_GET['et_pb_search_cat'] );
            $query->set( 'category__not_in', $categories_array );
        }
        if ( isset( $_GET['et-posts-count'] ) ) {
            $query->set( 'posts_per_page', (int) $_GET['et-posts-count'] );
        }
    }
}

I hope that any expert out there might help me to solve my problem

Upvotes: 0

Views: 61

Answers (0)

Related Questions