Support

Account

Home Forums General Issues Adding custom fields search to WordPress search Reply To: Adding custom fields search to WordPress search

  • it works for me

    function custom_search_query( $query ) {
        if ( !is_admin() && $query->is_search ) {
    		$result = $query->query_vars['s'];
    		$query->query_vars['s'] = '';
            $query->set('meta_query', array('relation' => 'OR',
                array(
    				'key'     => 'acf_name', // ACF FIELD NAME OR POST META
    				'value'   => $result,
    				'compare' => 'LIKE',
                )
            ));
            $query->set('post_type', 'servicios'); // optional POST TYPE
        }
    }
    add_filter( 'pre_get_posts', 'custom_search_query');