• Home
  • What We Do
  • Our Work
  • eCommerce Solutions
    • Ecommerce Platforms
      • Shopify
      • Shopify plus
      • Magento eCommerce
    • Growth Opportunities
      • Klaviyo Email Marketing
      • Shopify Support and Success
      • Shopify Migration Experts
      • eCommerce Strategy
      • eCommerce SEO
      • Wholesale B2B Solutions
      • Shopify & Yotpo Loyalty Solutions
      • ERP & Shopify Integrations
      • Shopify POS Solutions
  • COMPANY
  • Contact
GET A FREE QUOTE
Alinga
logo
Phone Call Icon
logo
  • What We Do
  • Our Work
  • eCommerce Solutions
  • Company
  • Shopify Plus Partner
UNLOCK YOUR GROWTH
  • Shopify Support and Success
  • Klaviyo Email Marketing
  • eCommerce Strategy and Growth
  • Shopify Migration Experts
  • Shopify Search Engine Optimisation
  • Wholesale B2B Solutions
  • Shopify POS Solutions
GET A FREE QUOTE
  • Home
  • Web
  • 4 Steps to Build an Advanced WordPress Search Making Use of WP_Query
WebJanuary 31, 2017

4 Steps to Build an Advanced WordPress Search Making Use of WP_Query

By Alinga

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.

Contact us today to get started!

Previous 3 Proven Ways to Sort out Security Issues Related to Magento Store
Next Which is ideal for building an ecommerce store : Magento or WordPress?
   

Related Posts

Getting your Shopify SEO right from day one

September 2, 2019

5 Simple Ways to Fine Tune Your WordPress Website to Increase Speed

February 15, 2017

Let’s Analyse 2017: Custom Website Development VS Web Building Tool

April 12, 2017

Massive Sydney party retailer launches new Shopify website

September 13, 2018
Group-1-copy
Overall 4.9 out of 5.0 client rating. 310+ clients on over 2500+ projects. view our work.
  • About Alinga

    We are a extremely talented, professional and incredibly knowledgeable team that produces high converting web solutions for growth businesses.

    Alinga - Shopify Expert Gold-Klaviyo-Master-White Yotpo-Winners-Badge
  • Business Solutions

    • COMPANY
    • WHAT WE DO
    • OUR WORK
    • CONTACT
    • BLOG
    • WEB DESIGN
    • SHOPIFY ECOMMERCE
    • SHOPIFY PLUS
    • MAGENTO ECOMMERCE
    • SHOPIFY SEO
    • ERP > SHOPIFY INTEGRATION
    • WHOLESALE B2B SOLUTIONS
    • SHOPIFY POS
    • SHOPIFY MIGRATION EXPERTS
    • KLAVIYO EMAIL MARKETING
© 2025 Alinga Web Media Design Pty Ltd. All rights reserved.
  • What We Do
  • Our Work
  • eCommerce Solutions
  • Company
  • Shopify Plus Partner