kisabelle
1/17/2014 - 10:00 PM

Search Custom Fields: Include Custom Fields in Wordpress Search

Search Custom Fields: Include Custom Fields in Wordpress Search

//-------------------------------------------------
//  Add Custom Fields to Default Wordpress Search 
//-------------------------------------------------
// add custom fields to search (does not work on ACF repeater fields)
function custom_search_where($pieces) {
    // filter to select search query
    if (is_search() && !is_admin()) {
        global $wpdb;
        $custom_fields = array('acknowledgements', 'the_proposal', 'the_big_day', 'favourite_moments', 'first_dance', 'newlywed_advice', 'honeymoon');
        $keywords = explode(' ', get_query_var('s'));
        $query = "";
        foreach ($custom_fields as $field) {
             foreach ($keywords as $word) {
                 $query .= "((mypm1.meta_key = '".$field."')";
                 $query .= " AND (mypm1.meta_value  LIKE '%{$word}%')) OR ";
             }
        }
        if (!empty($query)) {
            // add to where clause
            $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);
            $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
        }
    }
    return ($pieces);
}
add_filter('posts_clauses', 'custom_search_where', 20, 1);

/*
** Code from:
** http://wpdevsnippets.com/extend-search-include-custom-fields-without-plugin/
*/

// ******************************************************
// IMPORTANT: prevent duplicate posts being returned
// ******************************************************
function websmart_search_groupby( $groupby ) {
        global $wpdb;
        if( is_search() && !is_admin()) {
                $groupby = "$wpdb->posts.ID";
        }
        return $groupby;
}
add_filter('posts_groupby', 'websmart_search_groupby' );

/*
** Code from:
** http://websmartdesign.co.nz/searching-structured-post-data-with-wordpress/
*/

//   .dP"Y8 888888    db    88""Yb  dP""b8 88  88
//   `Ybo." 88__     dPYb   88__dP dP   `" 88  88
//   o.`Y8b 88""    dP__Yb  88"Yb  Yb      888888
//   8bodP' 888888 dP""""Yb 88  Yb  YboodP 88  88

//    dP""b8 88   88 .dP"Y8 888888  dP"Yb  8b    d8     888888 88 888888 88     8888b.  .dP"Y8
//   dP   `" 88   88 `Ybo."   88   dP   Yb 88b  d88     88__   88 88__   88      8I  Yb `Ybo."
//   Yb      Y8   8P o.`Y8b   88   Yb   dP 88YbdP88     88""   88 88""   88  .o  8I  dY o.`Y8b
//    YboodP `YbodP' 8bodP'   88    YbodP  88 YY 88     88     88 888888 88ood8 8888Y"  8bodP'

// ---------------------------
//  Search Custom Fields
// --------------------------- 


/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

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

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;
   
    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );