Home Forums General Issues Problem with searching/filtering text field


Problem with searching/filtering text field

  • I am new to ACF and loving it, but the learning curve is considerable.

    I am setting up a website for a client to sell second hand books. Once the site is up I will have to import a large database of over 20K+ books.

    I am using ACF to add additional information to the books as woocommerce products, e.g. ISBN, Author, Publisher, Book Condition, etc. The trouble I’ve run into is this:

    I am setting up a custom search/filter page and it works however I’ve noticed that searching/filtering in relation to the Author custom filed only works (produces a result) if an EXACT match is entered. E.g. if the author is ‘Douglas Adams’ his books only show up if searching for/filtering with an exact match, but not if ‘Adams’ alone is entered. This is a problem as most customers will likely just type a surname in to find books by an author, not to mention that about 80% of the author names in the database follow a ‘surname, firstname’ convention. E.g Douglas Adams might have been entered into that database as ‘Adams, Douglas’.

    Is there a way of modifying this behaviour? What do I do?

    Any helps/comments would be much appreciated and like I said, I am a newbie so perhaps I am just missing something obvious?

  • You will have to search the field the same way that WP does a search. WP splits the input into words does a LIKE query on each word.

    It is a complex operation. See:

    FILE: …/wp-includes/class-wp-query.php

    LINE: ~1358

    protected function parse_search( &$q ) {
  • Thank you John,

    Clearly this is the situation when one of us knows what he is talking about and the other one doesn’t. To be clear: you are the one that knows! 🙂

    I am afraid your reply left me none the wiser. Is this something that could be tackled through functions.php? I think you are suggesting, and this bit I get, is that the issue is around the input being split into words and then doing a LIKE query on each word, which is what WP apparently does. But that this is not what’s happening with ACF? How do I tackle it?

  • I only have an idea of what needs to be done. If you look at the code in WP that I mentioned you’ll see a complex regular expression that WP uses to split a string into words. It splits the string and also removes characters that you don’t want to search by.

    How are you doing the search/filtering now? What are you using?

  • Thank you John, I realise you are trying to help.

    I’ve set up a Field Group containing fields for additional book information (e.g. publisher, publishing year, author, Title, edition no., etc.) which is tied to woocommerce products. Everything works fine woocommerce, wise, etc. but I also need to create and advanced search page or section where customers can search for books.

    Above I’ve explained the issue and you are clearly right when you say the issue WP (its search) splits a string into words but this doesn’t seem to be the case with ACF. I’ve tried various plugins for filtering, inc. JetSmart Filters, Filter Everything, Product Filter by WooBeWoo, etc. but the issue remains. When trying to search via the Author custom field, the search only works with EXACT match, which is kind of a disaster from my point of view.

    I am just surprised that I haven’t found anyone else having the same problem. I would have thought with the amazing capabilities of ACF, someone would have come up with a way to build a search page where you’d be able to change the conditional logic, etc. And that you would have the option to decided whether you want an EXACT match or partial match on a custom field string. Is this a limitation of ACF perhaps?

    Anyhow, thank you for your time and effort and sorry if I am being too much of a newbie.

  • When it comes to WC I generally try to use product attributes instead of ACF fields and then use existing search plugins for searching products.

    There are plugins available that will can be used to include custom fields in the WP keyword search.

    I usually go a different path when it comes to keyword searching and ACF.

    but for creating a search field that specifically searches a single field by keyword, I have not seen anything like this, all I can do is point you in the right direction. Like I said, you need to split the input into words on spaces and punctuation, I would probably try something simple like

    $parts = preg_split('/\W/1', $string)

    This will split up a string on any non word character. Then you build the query that does a LIKE query on each for each part. It would not be perfect, but it would get the job done.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.