Support

Account

Home Forums Front-end Issues Adding custom fields to keyword search Reply To: Adding custom fields to keyword search

  • Just a heads up that the code linked to by John has an error. The code should only target the current query, however due to the use of the global is_search() function, the filter will be applied to all queries on page load that happens to be a search.

    The first snippet should be:

    
    function cf_search_join( $join, $query ) {
    
      global $wpdb;
    
      if ( $query->is_search ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
      }
    
      return $join;
    
    }
    add_filter('posts_join', 'cf_search_join', 10 , 2 );
    

    Same goes forth for posts_where and posts_distinct.