WordPress does accompany a number of superpowers that attributes to the data architecture highly flexible in nature. This in turn helps developers to have customized installations with custom fields, taxonomies, and post types. However, with just one field form search availability, WordPress does force developers to take help from external searches likes third party plugins, Google custom search, and others.
Do not worry. We have good news for developers who are striving for advanced search functionality in WordPress. Let us see how to devise an advanced search form feature to search as well as retrieve the custom results, through filtering of multiple field values and taxonomy terms.
Applying theoretical approach to coding
Step 1: Setting up the data structure
Custom post type purpose is to add content logically, not included in static pages or not present in blog posts. Such custom post types are apt for catalogue items, movies, books, products, events, and so on. Let us build the real estate WordPress site search using the structure.
custom post type:Â accommodation;
custom taxonomy: typology (hotel, homestay, B&B, and more)
custom field: _sm_accommodation_type (shared room, private room, entire house)
custom field:Â _sm_accommodation_city
other custom fields
It is time now to register the post type, Meta boxes, custom fields, and custom taxonomy. Even Edit Accommodation appears at least once containing custom taxonomy for Typology with several registered custom fields. After defining the structure for data, it is time now for query variables registration.
Step 2: Registering variables (VAR) for query
Earlier we had key=value defining the query vars, succeeded by a question mark. However, it is time now to register them through a functions file or as a plugin. As far as our purpose goes, we need two variables enabling query execution based on corresponding custom field values.
/**
* Register custom query vars
*
* @link https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
*/
function sm_register_query_vars( $vars ) {
$vars[] = ‘type’;
$vars[] = ‘city’;
return $vars;
}
add_filter( ‘query_vars’, ‘sm_register_query_vars’ );
So you see, we just queried the database by two parameters added up. It is wise to have a URL like this now getting build:
http://yoursite.com/?type=AAA&city=BBB
Step 3: Manipulation of actual queries
/**
* Build a custom query based on several conditions
* The pre_get_posts action gives developers access to the $query object by reference
* any changes you make to $query are made directly to the original object – no return value is requested
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
*
*/
function sm_pre_get_posts( $query ) {
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || ! $query->is_main_query() ){
return;
}
// edit the query only when post type is ‘accommodation’
// if it isn’t, return
if ( !is_post_type_archive( ‘accommodation’ ) ){
return;
}
$meta_query = array();
// add meta_query elements
if( !empty( get_query_var( ‘city’ ) ) ){
$meta_query[] = array( ‘key’ => ‘_sm_accommodation_city’, ‘value’ => get_query_var( ‘city’ ), ‘compare’ => ‘LIKE’ );
}
if( !empty( get_query_var( ‘type’ ) ) ){
$meta_query[] = array( ‘key’ => ‘_sm_accommodation_type’, ‘value’ => get_query_var( ‘type’ ), ‘compare’ => ‘LIKE’ );
}
if( count( $meta_query ) > 1 ){
$meta_query[‘relation’] = ‘AND’;
}
if( count( $meta_query ) > 0 ){
$query->set( ‘meta_query’, $meta_query );
}
}
add_action( ‘pre_get_posts’, ‘sm_pre_get_posts’, 1 );
The above block of code sums up initial part of our discussion. Herein, the function for callback quits if user is present within the admin panel. This is especially true if main query is not current query with accommodation is not the required post type.
After that the query is checked for an earlier registration or note. This is accomplished through get_query_var, making use of an HTTP request to retrieve public query variable. In the light of variable availability, the callback first defines a separate array for every single Meta query, and then pushes the same into multidimensional query through $meta_query array.
So now there are at least two Meta queries available with relation argument pushed right into $meta_query, with AND value. After things are done, $query is saved by set method for continuous execution. Herein, you do not need any data sanitization since WP_Meta_Query and WP_Query classes do the job.
Step 4: Finally time to build the Search Form
GET method is used to submit form data. This clearly demonstrates value and name form field attributes sent as URL variables. Hence, name attribute of form field have same values as registered previously as query vars like type and city. Also, field values can be automatically assigned, retrieving database, or things filled by user.