• Leon

    (@theawaken83)


    [On my website,]?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'] );
            }
        }
    }

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hello @theawaken83

    To fix the WooCommerce product search issue on your website, try this simplified approach:

    1. Remove your current code from functions.php.
    2. Add this code to include product post types and tags in the search:
       function wpz_search_filter($query) {
           if (!is_admin() && $query->is_search) {
               $query->set('post_type', array('product'));
               $query->set('tax_query', array(
                   array(
                       'taxonomy' => 'product_tag',
                       'field'    => 'name',
                       'terms'    => $query->query_vars['s'],
                       'operator' => 'LIKE',
                   ),
               ));
           }
       }
       add_action('pre_get_posts', 'wpz_search_filter');

    This should help WooCommerce search results include products with relevant tags.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.