Расширил стандартный поиск Wordpress вот такими функциями:
// Join posts and postmeta tables
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() && !is_admin() ) {
$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
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() && !is_admin() ) {
$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' );
//Incude comments in search
function search_comments_join($join) {
global $wpdb;
if( is_search() && !is_admin() ) {
$join .=' LEFT JOIN '.$wpdb->comments.' ON '.$wpdb->posts.'.ID = '.$wpdb->comments.'.comment_post_ID ';
}
return $join;
}
add_filter('posts_join', 'search_comments_join');
function search_comments_where($where) {
global $wpdb;
if( is_search() && !is_admin() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->comments.".comment_author LIKE $1) OR (".$wpdb->comments.".comment_content LIKE $1)",
$where
);
}
return $where;
}
add_filter('posts_where', 'search_comments_where');
// Prevent duplicates
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() && !is_admin() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
//Limit posts in search results
function limit_posts_per_search_page() {
if ( is_search() && !is_admin() )
set_query_var('posts_per_archive_page', 10);
}
add_filter('pre_get_posts', 'limit_posts_per_search_page', 9999);
Однако теперь поиск работает медленно, как оптимизировать?