我的要求是我必须修改wordpress自定义搜索以包含自定义分类。
您可以在我的functions.php
但是当我尝试在我的暂存环境中执行此代码时 网址为http://panolam.staging.wpengine.com/?s=metal
我收到502 Bad Gateway nginx错误。当我检查wpengine上的错误日志时,我发现了这个:
PHP警告:mysqli_query():MySQL服务器已经在1931行的/nas/content/staging/panolam/wp-includes/wp-db.php中消失了
它表明某些查询需要花费很多时间或其他任何东西。
我在google上寻找解决方案,发现在my.ini MySQL设置中将max_allowed_packet更改为16M而不是1M,但我不想这样做。任何人都可以告诉我可以在代码中改进哪些内容以防止出现此问题吗?
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() && !is_admin()) {
$join .= "LEFT JOIN $wpdb->postmeta AS m ON ($wpdb->posts.ID = m.post_id) ";
}
return $join;
}
function websmart_search_groupby( $groupby ) {
global $wpdb;
if( is_search() && !is_admin()) {
$groupby = "$wpdb->posts.ID";
}
return $groupby;
}
function websmart_search_where( $where ) {
global $wpdb, $wp_query;
if( is_search() && !is_admin()) {
$where = "";
$search_terms = se_get_search_terms();
$n = !empty($wp_query->query_vars['exact']) ? '' : '%';
$searchand = '';
if (count($search_terms) < 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->esc_like( $term );
// Get term by name in Custom taxonomy.
$term_details = get_term_by('name', $term, 'pattern_type');
//echo '<pre>'; print_r($term_details) ; die();
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}') OR (m.meta_value LIKE '{$n}{$term_details->term_id}{$n}'))";
$searchand = ' AND ';
}
}
$where .= " AND ${search} ";
$where .= " AND (m.meta_key IN ('pattern_type')) ";
$where .= " AND ($wpdb->posts.post_type IN ( 'post', 'page', 'product')) ";
$where .= " AND ($wpdb->posts.post_status = 'publish') ";
}
return $where;
}
// Code from Search Everywhere plugin
function se_get_search_terms()
{
global $wpdb, $wp_query;
$s = isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '';
$sentence = isset($wp_query->query_vars['sentence']) ? $wp_query->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('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
$search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
}
}
return $search_terms;
}
答案 0 :(得分:0)
我对我的代码做了一些更改,虽然看起来很多代码但是它的工作正常符合我的要求。
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() && !is_admin()) {
$join .= "LEFT JOIN $wpdb->postmeta AS m ON ($wpdb->posts.ID = m.post_id) ";
$join .= "LEFT JOIN $wpdb->term_relationships AS tr ON ($wpdb->posts.ID = tr.object_id) ";
$join .= "LEFT JOIN $wpdb->term_taxonomy AS tt ON (tt.term_taxonomy_id=tr.term_taxonomy_id) ";
$join .= "LEFT JOIN $wpdb->terms AS t ON (t.term_id = tt.term_id) ";
}
return $join;
}
function websmart_search_groupby( $groupby ) {
global $wpdb;
if( is_search() && !is_admin()) {
$groupby = "$wpdb->posts.ID";
}
return $groupby;
}
function websmart_search_where( $where ) {
global $wpdb, $wp_query;
if( is_search() && !is_admin()) {
$where = "";
$search_terms = se_get_search_terms();
$n = !empty($wp_query->query_vars['exact']) ? '' : '%';
$searchand = '';
if (count($search_terms) < 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->esc_like( $term );
// Get term by name in Custom taxonomy.
$term_details = get_term_by('name', $term, 'pattern_type');
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}') OR (m.meta_value LIKE '{$n}{$term_details->term_id}{$n}'))";
$searchand = ' AND ';
}
}
$where .= " AND {$search} ";
$where .= " AND (m.meta_key IN ('pattern_type')) ";
$where .= " AND ($wpdb->posts.post_type IN ( 'post', 'page', 'portfolio_page', 'product', 'cptbc', 'technical_doc', 'whats_new')) ";
$where .= " AND ($wpdb->posts.post_status = 'publish') ";
}
return $where;
}
// Code from Search Everywhere plugin
function se_get_search_terms()
{
global $wpdb, $wp_query;
$s = isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : '';
$sentence = isset($wp_query->query_vars['sentence']) ? $wp_query->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('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches);
$search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
}
}
return $search_terms;
}