Advanced Custom Fields combined with Ultimate WP Query Search Filter

This is the complete results of all the parts on the previous page, plus some extra features I picked up on the 9-sec forums.

  1. // overwrite search plugin output
  2. function customize_ultimate_output($results , $arg, $id, $getdata ) {
  3.  
  4.     $city           = $arg['meta_query'][1]['value'];
  5.     $city_compare   = $arg['meta_query'][1]['compare'];
  6.     $sex            = $arg['meta_query'][2]['value'];
  7.     $sex_compare    = $arg['meta_query'][2]['compare'];
  8.     $age            = $arg['meta_query'][3]['value'];
  9.     $age_compare    = $arg['meta_query'][3]['compare'];
  10.     if ( isset($arg['meta_query'][4]['value']) ) {
  11.         $services_key       = $arg['meta_query'][4]['key'];
  12.         $services_compare   = $arg['meta_query'][4]['compare'];
  13.         $services_value     = $arg['meta_query'][4]['value'];
  14.  
  15.         // check count of array
  16.         if ( count($services_value) == 1 ) {
  17.             $service_value = implode(',', $services_value);
  18.             $services_array = array(
  19.                 array(
  20.                     'key'       => 'sd_services',
  21.                     'value'     => $service_value,
  22.                     'compare'   => 'LIKE',
  23.                 ),
  24.             );
  25.         } else {
  26.  
  27.             $service_array = array();
  28.             foreach ($services_value as $service_value) {
  29.                 $service_array[] = array(
  30.                     'key'       => 'sd_services',
  31.                     'value'     => $service_value,
  32.                     'compare'   => 'LIKE',
  33.                 );
  34.             }
  35.  
  36.             // add array of values with OR statement
  37.             $services_array = array(
  38.                 // 'relation' => 'OR',
  39.                 $service_array
  40.             );
  41.         }
  42.     }
  43.  
  44.     $search_term    = $arg['s'];
  45.     $posts_per_page = $arg['posts_per_page'];
  46.     $post_type      = $arg['post_type'];
  47.     $post_status    = $arg['post_status'];
  48.     $orderby        = $arg['orderby'];
  49.     $order          = $arg['order'];
  50.     $meta_key       = $arg['meta_key'];
  51.     $meta_query     = array();
  52.     $tax_query      = array();
  53.     $compare        = '';
  54.     $paged          = false;
  55.     $meta_key       = NULL;
  56.  
  57.     if ( isset($services_key) ) {
  58.  
  59.         $new_args = array(
  60.             'post_status'       => 'publish',
  61.             'post_type'         => $post_type,
  62.             'posts_per_page'    => $posts_per_page,
  63.             'meta_key'          => $meta_key,
  64.             'orderby'           => $orderby,
  65.             'order'             => $order,
  66.             'paged'             => $paged,
  67.             'meta_query'        => array(
  68.                 'relation'      => 'AND',
  69.                 array(
  70.                     'key'       => 'sd_city',
  71.                     'value'     => $city,
  72.                     'compare'   => $city_compare,
  73.                 ),
  74.                 array(
  75.                     'key'       => 'sd_sex',
  76.                     'value'     => $sex,
  77.                     'compare'   => $sex_compare,
  78.                 ),
  79.                 array(
  80.                     'key'       => 'sd_age',
  81.                     'value'     => $age,
  82.                     'compare'   => $age_compare,
  83.                 ),
  84.                 $services_array
  85.             ),
  86.             's' => '',
  87.         );
  88.  
  89.     } else {
  90.         $new_args = $arg;
  91.     }
  92.  
  93.     $apiclass   = new uwpqsfprocess();
  94.     $wp_query   = new WP_Query( $new_args ); // get posts with new args
  95.     ob_start();
  96.     $result     = '';
  97.  
  98.     // The Loop
  99.     if ( $wp_query->have_posts() ) :
  100.         $countquery = $wp_query->found_posts;
  101.         $what       = 'result';
  102.         if ( $countquery > 1 ) {
  103.             $what .= 's';
  104.         }
  105.  
  106.         echo '<section class="section_something">';
  107.         echo '<div class="container">';
  108.  
  109.             echo '<p>We found '.$countquery.' ' . $what . ' matching your criteria.</p>';
  110.  
  111.             while ( $wp_query->have_posts() ) : $wp_query->the_post();
  112.                 // stuff to display a result
  113.             endwhile;
  114.  
  115.             echo $apiclass->ajax_pagination( $arg['paged'], $wp_query->max_num_pages, 20, $id, $getdata );
  116.  
  117.         echo '</div>';
  118.         echo '</section>';
  119.  
  120.  
  121.     else : // if no posts
  122.         echo '<p>NOTHING</p>';
  123.     endif;
  124.  
  125.     /* Restore original Post Data */
  126.     wp_reset_postdata();
  127.     $results = ob_get_clean();
  128.     return $results;
  129. }
  130. add_filter('uwpqsf_result_tempt', 'customize_ultimate_output', '', 4);
  131.  
  132. function uwpqsf_var($s) {
  133.     if (is_search() && isset($_GET['s']) && $_GET['s'] == 'uwpsfsearchtrg' && isset($_GET['uformid']) ) {
  134.         if (isset($_GET['taxo'])) {
  135.             foreach ($_GET['taxo'] as $v) {
  136.                 if (isset($v['term'])) {
  137.                     if ($v['term'] == 'uwpqsftaxoall') {
  138.                     } else {
  139.                         $termname = get_term_by('slug',$v['term'],$v['name']);
  140.                         $var[] = $termname->name;
  141.                     }
  142.                 }
  143.             }
  144.             if (!empty($_GET['skeyword'])){
  145.                 $var[] = $_GET['skeyword'];
  146.             }
  147.             $return = '';
  148.             if (!empty($var)) {
  149.                 $return = implode(' | ', $var);
  150.             }
  151.             return $return;
  152.         }
  153.     } else {
  154.         return $s;
  155.     }
  156. }
  157. add_filter( 'get_search_query', 'uwpqsf_var', 20, 1 );
  158.  
  159. // add AND to taxonomy and free search
  160. function enable_taxo_key($tax_query , $get_tax, $keyword){
  161.     return $get_tax;
  162. }
  163. add_filter('barg_tax_query', 'enable_taxo_key','', 3);
  164.  
  165. // add AND to meta fields and free search
  166. function enable_metafield_key($meta_query , $get_meta, $keyword){
  167.     return $get_meta;
  168. }
  169. add_filter('barg_meta_query', 'enable_metafield_key','', 3);