How to customize the search form

How to manually customize the search form

This tutorial is recommended for advanced users only, however if you are a beginner and you follow every step in the right way you'll be able to customize the search form.

Start by opening the file that you wish to modify, the theme comes with 2 search forms, a regular one that can be added through widgets and/or shortcodes and the advanced search form visible only in taxonomies pages.

  1. If you wish to modify the regular search form modify file: search.php in folder /framework/extensions/
  2. If you wish to modify the advanced search form, modify file: advanced-search.php in folder /framework/extensions/

Both files share the same "concept" so the instructions available here apply to both forms.

Open your prefered file and you'll notice a function/code that looks like this

 

/*   					 * Register search form fields  					 */  					if(!function_exists('tdp_search_fields')) {  					    function tdp_search_fields() {    					    	/*   							 * Generate Array of all main vehicle models  							 */  					    	$args = array(  								'type'                     => 'vehicles',  								'child_of'                 => 0,  								'parent'                   => 0,  								'orderby'                  => 'name',  								'order'                    => 'ASC',  								'hide_empty'               => 0,  								'hierarchical'             => 1,  								'exclude'                  => '',  								'include'                  => '',  								'number'                   => '',  								'taxonomy'                 => 'vehicle_model',  								'pad_counts'               => false     							);    							$default_option = array('-1' => __('Select a Maker »','framework'));    					    	$output_categories = array();  							$categories=get_categories($args);  							  foreach($categories as $category) {   							     $output_categories[$category->cat_ID] = $category->name ;  							}    							$all_vehicles = $default_option + $output_categories;    					    	/*   							 * Generate Fields For The Search Form  							 */  					    	$args = array();  					        $args['form'] = array('action' => get_field('search_results_page','option'));  					          					        $args['wp_query'] = array(  					            'post_type' => 'vehicles',  					            'posts_per_page' => get_field('results_per_page','option'),  					            'order' => 'DESC',  					            'orderby' => 'date'  					        );    							$args['fields'][] = array(  					            'type' => 'html',  					            'value' => tdp_search_ajax()  					        );    					        $args['fields'][] = array(  					              'type' => 'generic',  					              'id' => 'main_cat',  					              'label' => __('Select A Maker:','framework') . '',  					              'format' => 'select',  					              'default' => '',  					              'values' => $all_vehicles,  					        );    							$args['fields'][] = array(  					              'type' => 'generic',  					              'id' => 'tax_vehicle_model',  					              'label' => __('Select Model:','framework') . '',  					              'format' => 'select',  					              'default' => '',  					              'values' => array('' => __('Select A Maker First','framework')),  					        );    							//verify the field is enabled  					        if(get_field('display_price_filter','option')) {  							  						        $args['fields'][] = array(  						            'type' => 'generic',  						            'label' => __('Price Filter','framework') . '',  						            'id' => 'range_1',  						            'format' => 'text',  						        );    					    	}    					    	///// MORE SEARCH FIELDS ARE AVAILABLE HERE  

This is the function that you have to modify in order to add new fields. It is recommended that you add your own fields just above this snippet here:

 

$args['fields'][] = array(  		            'type' => 'submit',  		            'value' => __('Filter Vehicles »','framework')  		        );

There are two essential components to any arguments array:

  1. WP_Query Arguments – Established with the ‘wp_query’ key, these arguments set the defaults for your search’s WP_Query object. Any arguments accepted by WP_Query may be passed here.
  2. Fields – Fields can be added to your search form by creating & extending the ‘fields’ key of your arguments array. Each value in the ‘fields’ array contains an array storing arguments for that field. Fields will be generated on the page in the order they are defined. Please refer to the Fields documentation for more in-depth instructions on establishing your form fields.

Field Types

Fields can be added to your search form by creating & extending the ‘fields’ key of your arguments array. Each value in the ‘fields’ array contains an array storing arguments for that field. Fields will be generated on the page in the order they are defined.

There are a variety of field types available for use in your search forms. They are:

  1. author
  2. date
  3. generic
  4. html
  5. meta_key
  6. order
  7. orderby
  8. posts_per_page
  9. post_type
  10. search
  11. submit
  12. taxonomy

Field types are defined by passing the ‘type’ argument within the field’s arguments array.

Example: Creating a taxonomy field

 

$args['fields'][] = array('type' => 'taxonomy');

Fields Format

Fields can be constructed in a variety of formats, such as: text inputs, select boxes, checkboxes, radio buttons, and multi-select. You can specify a field’s format by passing a ‘format’ parameter to the field’s arguments:

$args['fields'][] = array('type' => 'taxonomy', 'format' => 'select');

The above code will create a taxonomy field as a select box. For more information, consult the documentation on field formats & parameters below here.

 

author field

A field used for searching posts by author.

Parameters

label
( string)   
Default: None
format
( string)   'select', 'multi-select', 'checkbox', 'radio', 'text', 'hidden'
Default: select
default
( string)   
Context: A user ID number
Default: None
authors
( array)   An array of integers (author IDs)
Default: An array of all users (ID => name)

date field

Parameters

label
( string)   
Default: None
format
( string)   'select', 'multi-select', 'checkbox', 'radio', 'text', 'hidden'
Default: select
date_type
( string)   'year', 'month', 'day'
Default: year
date_format
( string)   A valid PHP date format string. See this page for details.
Default: Varies by date_type. (year: 'Y', month: 'M Y', day: 'M j, Y')
values
( array)   
Default: Varies by date_type. An array of all [years/months/days] in which content has been published.
default
( string)   
Default: None

meta_key field

A field used for searching posts by post meta.

Parameters

label
( string)   
Default: None
format
( string)   'select', 'multi-select', 'checkbox', 'radio', 'text', 'hidden'
Default: select
meta_key
( string) ( required)   
Default: None
values
( array) ( required)   
Default: None
compare
( string)    '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS'
Default: '='
default
( string)   
Default: None

search

A field used for standard keyword-based search queries.  Search fields are always implemented as text fields.

Parameters

label
( string)   
Default: None
format
( string)   'select', 'multi-select', 'checkbox', 'radio', 'text', 'hidden'
Default: text
value
( string)   
Default: None
placeholder
( string)   
Default: None

submit

Displays a submit button for the search form.

Parameters

label
( string)   
Default: None
value
( string)   
Default: Search

taxonomy

A field used for searching posts by specified terms in a given taxonomy.

Parameters

label
( string)   
Default: None
format
( string)   'select', 'multi-select', 'checkbox', 'radio', 'text', 'hidden'
Default: select
taxonomy
( string)   Taxonomy slug (ie 'category', 'post_tag')
Default: category
term_format
( string)   'ID', 'slug', 'name'
Default: 'slug'
terms
( array)   Array of term values, in the format specified by term_format (either ID, slug, or name)
Default: All terms belonging to the given taxonomy
operator
( string)   'IN', 'NOT IN', 'AND'
Default: AND
term_args
( array)   
Default: array('hide_empty' => false )
default
( string)   
Default: None