Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search only in custom field
This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko 2 years ago.
- AuthorPosts
- March 16, 2021 at 5:49 pm #32185
Dear Ajax Search Pro,
I need to implement the same functionality of “Front end search settings -> Generic filters” but with custom fields:
Instead of “search in title” I need to “search in book_name”; so what i type on the search bar is searched only on that custom field, for example: book_name or table_of_content.
To give you a broader picture, I have 2 custom pods named book and book_author and I need to filter the content with “Search (that string only) in book.book_name_tibetan” and “Search (that string only) in book.table_of_contents” and “Search (that string only) in author.author_name”
I’ve :
– set on General tab to search on books and book_authors, and specified the custom fields that i want to search for
– The “Front end search settings -> Generic filters” would be just that if i could specify custom fields
– In “Front end search settings -> Content types / Post Types” doesn’t look right
– In “Front end search settings -> Custom fields” you can search for the custom fields (they populate as you write) but the values section needs to be what has been typed in the search bar, not existing values of the database, or a custom setHow can I implement a “Search this string” and then filter with a “Search this string in this custom field”?
Thank you
March 17, 2021 at 10:31 am #32196Hi,
The problem with this is, that the Generic Filters does not support a “search in custom fields” feature. It is not possible to control that from the front-end.
Best,
There are custom field based filters, but as you said, they work differently. You can choose the fields to search on the General Options -> Sources panel, but those will be always searched.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 17, 2021 at 5:25 pm #32202How do i customize the plugin to search in the custom fields? is there any documentation ?
Thank you
March 18, 2021 at 9:25 am #32209Sure!
There is the asp_query_args hook, which lets you change the search query arguments. The post_custom_fields sub-argument is used to define the custom fields to search. You can also check which fields are selected to search for via the post_fields argument.
Theoretically, you can use that to check if the “title” is selected for search, and add custom fields of your choice to the post_custom_fields array, something like this:
Best,add_filter('asp_query_args', 'asp_add_fields_by_filters', 10, 1); function asp_add_fields_by_filters( $args ) { // Title is selected for search if ( in_array('title', $args['post_fields'] ) ) { $args['post_custom_fields'] = array_unique(array_merge( args['post_custom_fields'], array('my_field_1', 'my_field_2') // -> add these fields for search )); // Optionally, you can even unset the title from searching afterwards array_diff( $args['post_fields'], array('title') ); } return $args; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.