• Hi,

    I’ve encountered a severe problem with the basic search functionality of wordpress in my woocommerce store (behavior of WP_Query::parse_search()).

    The story:
    The basic search does not look for product attributes values matching the string, so I needed a fix to let the users find what they are looking for (e.g. color of an item). My approach was, to keep the search string consistent (as the user typed it), look for the color terms, add them as a taxonomy query at runtime and also fill them into the stopwords array.

    So a user gets “T-Shirt green” shown in his search box and green is ignored (because it would yield zero results in content/excerpt). I am not able to buy one of the many relevance search plugins because they do not work together with my ajax filters.

    The problem:
    If a user seaches only for a color (e.g. “green”) the search-string is matched against the stopwords and a very inconsistent behaviour kicks in:

    https://developer.www.ads-software.com/reference/classes/wp_query/parse_search/
    line 1267 + 1268

    if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
                    $q['search_terms'] = array( $q['s'] );

    The search query is not sent as “empty” (as someone manipulating stopwords would expect = all results) but it ignores the stopwords.

    Possible solution:

    I would like to be able to choose between two options, where the behavior now is the default functionality:
    – treat all-stopword / long search as simple string (like it is now)
    – return all posts as result when only stopwords are used (like empty search)

    I am by no means a professional programmer but maybe you can add a filter to line 1268, I really don’t want to mess with core files and keep it update safe.

    Thank you for your time and effort,
    Chris

    Edit: My code can be seen at:

    • This topic was modified 6 years, 5 months ago by cedus. Reason: Added Code
    • This topic was modified 6 years, 5 months ago by Jan Dembowski.

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

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Filtering in WP doesn’t work the way you think it does. You cannot add a filter to arbitrary code lines, you can only add filters where the original developers include an apply_filters() call to which you can add filters. Because of this, it’s difficult to manipulate search parsing code. What you could do is hook the “posts_where” filter, with which you can alter the final SQL WHERE clause used for the query. You can add to, change, or completely replace the clause as required.

    The query object is passed to your callback. The original search text is available in WP_Query::query_vars[‘s’]

Viewing 1 replies (of 1 total)
  • The topic ‘Search: Result with only stopwords should be customizable’ is closed to new replies.