This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Select custom fields

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #52819
    NestorNestor
    Participant

    Hello. I need to make a search engine where I can choose to search selectively in a custom field (with a Drop down). There are 3 fields (ZEN Code, OFC Code, OR Original Code). I attach an example.
    The search must be done only on the selected option (using a Drop down to select a specific Custom Field).
    Each product may have a Custom Field with its ZEN Code, OFC Code and Original Code number, where applicable, or it may not have one).
    I don’t understand the logic of using the “Custom Fields” option in your Plugin and perhaps I should use another alternative for use this “select”.

    Could you give me some help to solve it please?

    I attach an example.

    Thank you so much.

    #52820
    NestorNestor
    Participant

    I attach an example.

    #52883
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    This could be possible via a bit of custom code and using a post meta filter, it’s not possible only via configuration.

    1. Make sure to use the regular engine
    2. Create a custom field filter exactly like this.
    3. Make it visible by default
    4. Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter('asp_query_args', function ( $args, $search_id, $options ) {
    	foreach ( $args['post_meta_filter'] as $key => $field ) {
    		if ( $field['key'] ==='dummy_field' ) {
    			$args['post_custom_fields_all'] = false;
    			$args['post_custom_fields'][] = esc_sql($field['value']);
    			unset($args['post_meta_filter'][$key]);
    		}
    	}
    	return $args;
    }, 10, 3);

    This will convert the filter into a search field instead.

    #52887
    NestorNestor
    Participant

    Thank you, Ernest!
    It works fine!
    Have a nice day/night.

    #52888
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.