Forums › Forums › Search & Filter Pro › Post Meta Wildcard
Tagged: post_meta wildcard
- This topic has 10 replies, 3 voices, and was last updated 9 years, 2 months ago by
Trevor.
-
Anonymous(Private) October 20, 2016 at 6:38 pm #65449
Hi,
First of all, this is a great plugin – well done! ๐
I have a list of posts (brands), and through ACF (advanced custom fields) each shop has a repeater field called address. This is because, each brand can have a number of shop outlets (classified in areas – north/central/south etc).
Thus for example:
Brand A – Shop 1 (Central), Shop 2 (North)
Brand B – Shop 1 (South), Shop 2 (Central), Shop 3 (North)In the database, these post meta are stored as address_0_area, address_1_area, address_2_area .. etc
One limitation i found was that the post meta you can only select a specific post meta. Thus you end up search in address_0_area only.
So if in the search form, i want to look up (via checkboxes) that i want shops in the North, it wont return anything, as for brand A, North is stored in address_1_area and for Brand B ‘North’ is stored in address_2_area.
One solution i had (i tried coding my way through before i found your plugin!) was that i modify the posts_where filter to include a wildcard, like so:
add_filter( 'posts_where' , 'allow_wildcards'); function allow_wildcards($where) { $where = str_replace("meta_key = 'address_%", "meta_key LIKE 'address_%", $where); return $where; }How can i go about it please?
Thanks! ๐
Trevor(Private) October 21, 2016 at 8:46 am #65518It depends on how you migrated the site. Search and Filter is quite sensitive and often a mysql dump will not do it right.
This may be the issue though:
Search and Filter Pro only correctly supports/searches/filters/caches simple ACF fields. The next version that we release will extend that support to ‘relationship’ fields. However, repeater fields are incredibly complex and difficult to support. It is out intention to further extend our support in future editions, but that may be a while away.
I do not know if there is a different way that you can structure the data (probably not as neat as the repeater field is)?
Anonymous(Private) October 21, 2016 at 9:59 am #65534Hi Trevor,
Dont worry re the mysql dump – i did it that way so as to be quick so you can have a look at the setup.
Re the repeater fields, i understand they are very complex. However an idea as i said is to put a wildcard where the user would be able to type in the variable name (e.g. in my case address_%_area) and next to it, tick a box LIKE. this way in the SQL, it would generate something like
‘WHERE meta_key LIKE ‘address_%_area’
This will then return all the values under for all the repeater fields, like address_0_area, address_1_area, address_2_area etc.
Would you please be able to point me in the right direction to the file where the SQL for post_meta is is being generated maybe i can have a go at it? ๐
Cheers ๐
Ross Moderator(Private) October 24, 2016 at 7:08 pm #66054Hey Fernand
Just to warn you wildcard searches can be very very slow, which is why we haven’t gone down that route within the plugin itself, or made it available…
Anyway, the way S&F is setup I think it would be very difficult to modify the behaviour.
I’ll try to explain.
S&F doesn’t do one big expensive query using lots of joins to fetch the results.
What it does is, it creates 2 tables in your setup.
First is a flat table with your taxonomy and meta data (search_filter_cache)..
Then S&F uses this table to build another –
search_filter_term_results, this basically stores the IDs against every option of the the fields in your search forms.So, in order to change the type of query as you mention, you would first need to change how S&F caches the posts.. specifically how it creates the values in the second table…
In addition to that, S&F has never supported repeater fields (its on the todo list), but essentially I don’t think S&F is actually able to index the data from repeater fields at all, so before you think about wildcard searches, I don’t think S&F can support repeater fields whatsoever in its current form…
If you did want to go diving in, the file you would need to modify is
search-filter-pro/includes/search-filter-post-cache.phpThen the function which is run to update the cache for a particular post is
update_post_cache… the function you would actually want to modify is:get_cache_term_idsHere you can find the SQL used to query first table I mentioned above (which then builds the values in the second table), which is where your wildcard stuff would need to go…
All in all, I think to do what you want would be extremely difficult.
Thanks
-
AuthorPosts