• Resolved nexlaa

    (@nexlaa)


    Hi, I am trying to search by post codes (UK). E.g. “SW3 5XS” So there is always a space between. The problem is, that Relevanssi is displaying all posts including the first term. It ignores the second term due to the space I guess. How can I manage this to consider the whole post code/term? Or is there a way to auto add a dash to the input field instead of spaces?

    Thanks! Alex

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Post codes are often a tricky challenge.

    Is your search used for something else, or just for post codes? Using a phrase search helps, if you search for "sw3 5xs" including the quotes, that should get you exact results.

    You can make a search field automatically do a phrase search if you add sentence="1" to the search input field. However, if the search is supposed to also search for something else than post codes, that may cause problems.

    One solution would be to recognise post codes and automatically wrap them in quotes to make them a phrase, but figuring out what is a valid UK post code is not an easy thing to do.

    Thread Starter nexlaa

    (@nexlaa)

    Hi Mikko,

    Thanks for you fast reply! Yep, it is used for UK post codes only so it should work with ?sentence=?1““ I will try at least.

    Thanks!
    Alex

    Plugin Author Mikko Saari

    (@msaari)

    If you’re only ever working with UK post codes, then another solution you can do is to eliminate all spaces in indexing and searching – but if the sentence solution works, it’s definitely less work.

    Thread Starter nexlaa

    (@nexlaa)

    That sounds good too. maybe we have to go this way. Could you tell me how to set this up? I guess a snippet in the functions.php?

    Regarding the sentence solution. Where or how do i add the ?sentence=?1“? I use WP Toolset and my code looks like this:

    [wpv-filter-search-box output=”bootstrap”]
    [wpv-filter-submit output=”bootstrap” class=”btn-secondary”][/wpv-filter-controls]

    Thanks for your support Mikko

    Plugin Author Mikko Saari

    (@msaari)

    You need to ask Toolset support about the sentence parameter; it’s quite possible you just can’t. But I know very little about Toolset, and the less I know, the happier I am. With Relevanssi search form shortcode, it’d be as easy as

    [searchform sentence="1"]

    To remove all spaces, you can do

    add_filter( 'relevanssi_punctuation_filter', 'rlv_remove_spaces' );
    function rlv_remove_spaces( $replacements ) {
        $replacements[' '] = '';
        return $replacements;
    }

    in the theme functions.php and rebuild the index.

    Thread Starter nexlaa

    (@nexlaa)

    Hi Mikko,

    thank you! The filter works like a charm!

    Thread Starter nexlaa

    (@nexlaa)

    Hi Mikko,

    just one more question:
    How can we manage to display all results starting with the same letter of the search field? E.g.: Search = “N1 0AX”
    It should display all results which includes “N*”
    So instead of adding all N* Postcodes (around 29.000) we would add “N*” as searchable custom field and this post should be displayed when one of the 29.000 post codes are searched like “N1 0AX”

    And also is it possible to display specific posts always no matter what users are searching for?

    Thank you so much,
    Alex

    • This reply was modified 4 years, 5 months ago by nexlaa.
    Plugin Author Mikko Saari

    (@msaari)

    If I understand this correctly, Relevanssi is not the right tool for the job. I’d implement the postcodes as a taxonomy and then create a function to map the postcodes to the taxonomy terms and just use WP_Query to browse the posts by taxonomy term. No point to involve Relevanssi in this, it’s just useless overhead.

    Thread Starter nexlaa

    (@nexlaa)

    Hi Mikko,
    apologies my late response. I have found a solution to the first question regarding the many Postcodes. However I am still looking for a way to display specific posts always on the search results no matter what’s a user is searching for. Is there a way?

    Thanks
    Alex

    Plugin Author Mikko Saari

    (@msaari)

    You can use the relevanssi_hits_filter filter hook to add posts to search results. It gives you an array of search results, it’s an array of post objects, and you can add, change and remove posts as you wish.

    Thread Starter nexlaa

    (@nexlaa)

    Thanks Mikko. Could you help me to customize the function so no matter what’s the searched term is it displays all posts containing “display-always” additional to the regular results?
    Appreciate your help!
    Alex

    Plugin Author Mikko Saari

    (@msaari)

    Where is the “display-always” contained? In a custom field, in a taxonomy or where?

    Anyway, all you need is to use get_posts() to fetch the posts you want to add to the results and in a relevanssi_hits_filter filter hook add then in the beginning of the hits array.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘searching for uk post codes (replace spaces with dashes in search input field)’ is closed to new replies.