• Your function uwpqsf_ajax HTML escapes the keyword (esc_html($keyword)). I am wondering why this is done since earlier it is run through the sanitize_text_field WP function? The problem this causes arises when someone searches for a phrase enclosed by quote marks because those quotation marks get converted to ". For example, say I search for "keyword phrase"

    The actual search will be for two words:
    "keyword
    and
    phrase"

    which invariably ends up returning no results.

    The WordPress native search already handles this well by recognizing such an entry as a phrase (and thus removing the quotation marks). Simply removing that esc_html part of your code would solve this problem. Unless there was a specific reason you included it that I am not considering?

    Now, for anyone that wants to change this, as I did, it can be done in your theme functions file using the following code:

    add_action( 'pre_get_posts', 'allow_phrase_searches', 9999 );
    function allow_phrase_searches( $query ) {
        if ( !is_admin() && $query->is_main_query() && $query->is_search ) {
    		$query->set( 's', html_entity_decode($query->query_vars['s']) );
        }
    }

    https://wordpress.org/plugins/ultimate-wp-query-search-filter/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author TC.K

    (@wp_dummy)

    Thanks for the tips.

    Thread Starter mojamba

    (@mojamba)

    Happy to help. By the way, now I think I know why you used the esc_html. Without it the text in the keyword text box won’t display properly after submission. One solution is to modify your $oldvalue (in searchform.php file, two different locations) from:

    $oldvalue = (isset($_GET['skeyword'])) ? $_GET['skeyword'] : '';

    to:

    $oldvalue = (isset($_GET['skeyword'])) ? stripslashes(esc_html($_GET['skeyword'])) : '';

    Plugin Author TC.K

    (@wp_dummy)

    Really appreciate your tips. Thanks. Will include it in the coming updates 🙂

    Thread Starter mojamba

    (@mojamba)

    Glad to help. Actually, I have been using UWPQSF as the foundation for my customized WP site search engine.and have been banging it around to suit my needs for the past 1-2 weeks. Along the way I have learned a lot and have made many changes. I think most or all of these changes could easily be incoporated and would be desired by other users who may not be able to do the coding necesary. I am happy to share all my code if you are interested in reviewing it and incorporating the features you think might be good additions. Below is a list of all the changes I have made so far:

    Back-end changes:

    • Allow phrase searches (enclosed in quotation marks)
    • Added ability to search excerpts (enabled by default but could easily add as a front-end option)
    • Added ability to keyword search taxonomy terms (and allow for excluding specific taxonomies)
    • Added ability to keyword search comments:
    1. user can enable/disable on front-end,
    2. user can choose search logic (AND/OR),
    3. by default only searches approved comments
    • Added ability to search by a date range
    • Added support for multi-select taxonomy filtering with hierarchical taxonomy listing (note: required some changes since multiselect creates an array where regular select creates a text variable)
    • Allow search logic based on user (front-end) input (currently can set logic for: core search, taxonomy search, comments search)
    • Order by relevance (only the basic WP relevance algorithm)
    • Search term highlighting for title and excerpt (semi-complicated to deal with allowing HTML tags in excerpts)
    • Integrated with Search Meter plugin to record search history

    Front-end changes:

    • Added display of search term to the results template title (e.g. # Results found for: <search terms>)
    • Added multiselect box (integrated with your admin screen)
    • Added date range with jQuery datepicker that is cross-browser compatible (core jQuery HTML5 datepicker is not well-supported)
    • Added ability for user to select the sort/orderby option
    • Added a Relevance option for sorting (on both admin screen and front-end selector)
    • Added front-end enable/disable keyword search for taxonomy terms
    • Added front-end enable/disable keyword search for comments
    • Added front-end selection of search logic to use for keyword search (three separate options: one for core keyword search, one for taxonomy search and one for comments search)
    • Added front-end selection of the number of results to show per page

    With the exception of the multiselect and relevance sorting (which integrates with the admin screen) I did most of the modifications manually via filters but some/all might make good admin screen options with the ability to select the layout position on the front-end form (as can currently be done with other fields).

    Other notes/suggestions:

    • You don’t currently apply a filter for tax_query ($get_tax) but this would be helpful for those who want to let users select the search logic (‘relation’, OR/AND). I am doing that now but have to modify the WHERE query whereas it would be cleaner to have a filter to set instead.
    • You apply a filter in post_type.php for the Post Type section of the admin screen, but not for the Result Setting and Others section (misc.php). This is, I think, where some new options like comment and excerpt searching, could logically be placed.
    • I didn’t do anything worth mentioning with respect to custom post types as they aren’t important to my site, but I think searching and sorting those should be even easier than taxonomies.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘searching for keyword phrases in quotation marks’ is closed to new replies.