title: 502 bad gateway on customising wordpress search to include ACF custom taxonomies
body: <p>My requirement is that I have to modify wordpress custom search to include custom taxonomies.</p>
  
  <p>You can find below Code that I am using for same in my <code>functions.php</code>.</p>
  
  <p>But when I am trying to execute this code on my staging environment 
  URL is <a href="http://panolam.staging.wpengine.com/?s=metal" rel="nofollow noreferrer">http://panolam.staging.wpengine.com/?s=metal</a></p>
  
  <p>I am getting 502 Bad Gateway nginx error. When I checked error logs on wpengine what I found this:</p>
  
  <blockquote>
    <p>PHP Warning: mysqli_query(): MySQL server has gone away in /nas/content/staging/panolam/wp-includes/wp-db.php on line 1931</p>
  </blockquote>
  
  <p>It shows that some query taking so much time or anything else it can be.</p>
  
  <p>I looked for solution for this on google and found that to change the max_allowed_packet to 16M instead of 1M in the my.ini MySQL settings, but I dont want to do this. Can anybody tell what I can improve in my code to prevent this issue?</p>
  
  <pre><code>add_filter('posts_join', 'websmart_search_join' );
  add_filter('posts_groupby', 'websmart_search_groupby' );
  add_filter('posts_where', 'websmart_search_where' );
  
  function websmart_search_join( $join ) {
      global $wpdb;
      if( is_search() &amp;&amp; !is_admin()) {
              $join .= "LEFT JOIN $wpdb-&gt;postmeta AS m ON ($wpdb-&gt;posts.ID      = m.post_id) ";
      }
      return $join;
  }
  
  function websmart_search_groupby( $groupby ) {
      global $wpdb;
      if( is_search() &amp;&amp; !is_admin()) {
              $groupby = "$wpdb-&gt;posts.ID";
      }
      return $groupby;
  }
  
  function websmart_search_where( $where ) {
      global $wpdb, $wp_query;
      if( is_search() &amp;&amp; !is_admin()) {
              $where = "";
              $search_terms = se_get_search_terms();
              $n = !empty($wp_query-&gt;query_vars['exact']) ? '' : '%';
              $searchand = '';
              if (count($search_terms) &lt; 1) {
                      // no search term provided: so return no results
                      $search = "1=0";
              } else {
                      foreach( $search_terms as $term ) {
                              //$term = esc_sql( like_escape( $term ) );
                              $term = $wpdb-&gt;esc_like( $term );
  
                              // Get term by name in Custom taxonomy.
                              $term_details = get_term_by('name', $term, 'pattern_type');
                              //echo '&lt;pre&gt;'; print_r($term_details) ; die();
                              $search .= "{$searchand}(($wpdb-&gt;posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb-&gt;posts.post_content LIKE '{$n}{$term}{$n}') OR (m.meta_value LIKE '{$n}{$term_details-&gt;term_id}{$n}'))";
                              $searchand = ' AND ';
                      }
              }
              $where .= " AND ${search} ";
              $where .= " AND (m.meta_key IN ('pattern_type')) ";
              $where .= " AND ($wpdb-&gt;posts.post_type IN ( 'post', 'page', 'product')) ";
              $where .= " AND ($wpdb-&gt;posts.post_status = 'publish') ";
      }
      return $where;
  }
  
  
  // Code from Search Everywhere plugin
  function se_get_search_terms()
  {
      global $wpdb, $wp_query;
      $s = isset($wp_query-&gt;query_vars['s']) ? $wp_query-&gt;query_vars['s'] : '';
      $sentence = isset($wp_query-&gt;query_vars['sentence']) ? $wp_query-&gt;query_vars['sentence'] : false;
      $search_terms = array();
  
      if ( !empty($s) )
      {
              // added slashes screw with quote grouping when done early, so done later
              $s = stripslashes($s);
              if ($sentence)
              {
                      $search_terms = array($s);
              } else {
                      preg_match_all('/".*?("|$)|((?&lt;=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
                      $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
              }
      }
      return $search_terms;
  }
  </code></pre>