WordPress Property Real Estate Plugin

Field System - Search Query Vars

When a custom property field is marked as allowed for searching there are couple of extra steps needed to register the query variables so it will be eligible when WP_Query is processing the actual query.

Step One – Register the searched variable key When Building the Property Field – search object , you have to define the form field name attribute . That attribute will be passed as either POST or GET entry key when user is submitting the form back to the server.

WordPress will ignore the passed key if it hasn’t been registered yet. Invoke hook filter query_vars to register the key to WordPress

Example of adding new query vars



/**
 * Invoking the query_vars filter hook
 */
add_filter('query_vars', 'my_new_variables');
function my_new_variables($variables) {
  $variables[] = 'property_awesome';
  return $variables;
}



Step Two – Registering the search logic

After succcessfully registering the query vars key into wordpress, the next step is to register the logic to process the value passed with the query vars key

You will need to register the logic via hook action pre_get_posts and hook action vtcore_wordpress_loop_process_query

Example to add new logic to pre_get_posts



/**
 * Invoking the query_vars filter hook
 */
add_filter('pre_get_posts', 'alter_query');
function alter_query($query) {

  // Only concern about main query and on front page
  if ($query->is_main_query() != false && !is_admin()) {
    
    // add the logic here
    if (isset($_REQUEST['mykey']) {
      $query->set('tax_query', ....);
    }

  }
}



Example to add new logic to vtcore_wordpress_loop_process_query



/**
 * Invoking the vtcore_wordpress_loop_process_query action hook
 */
add_filter('vtcore_wordpress_loop_process_query', 'alter_query');
function alter_query($object) {
  
  // We are on ajax mode
  if (isset($object->request['data']) {
    $request = VTCore_Wordpress_Utility::wpParseLargeArgs($object->request['data'];
    
    // VTCore_Wordpress_Element_WpLoop object will always have custom loop id
    // and need that custom unique loop id to differentiate one loop object to another
    // The property search form will always append that unique id in front of every
    // form field name attributes to determine the right target to search / filter.
    $id = $object->getContext('id');

    // Check if we got the right request for our custom field
    if (isset($request[$id . '_my_custom_field'])) {
       
      // Perform the custom logic
      // @see wordpress taxQuery and metaQuery for valid array format
      $object->taxQuery[] = ......
      $object->metaQuery[] = .......
    }
  }
}