Search only in custom field

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 Ernest Marcinko 3 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #32185
    tsadra96
    tsadra96
    Participant

    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 set

    How can I implement a “Search this string” and then filter with a “Search this string in this custom field”?

    Thank you

    #32196
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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.
    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #32202
    tsadra96
    tsadra96
    Participant

    How do i customize the plugin to search in the custom fields? is there any documentation ?

    Thank you

    #32209
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure!

    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:

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


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

You must be logged in to reply to this topic.