自定义 WordPress 搜索以包含 ACF 自定义分类法时出现 502 错误网关

问题描述 投票:0回答:1

我的要求是我必须修改 WordPress 自定义搜索以包含自定义分类法。

您可以在我的

functions.php
中找到我正在使用的以下代码。

但是当我尝试在我的临时环境中执行此代码时 网址是 http://panolam.staging.wpengine.com/?s=metal

我收到 502 Bad Gateway nginx 错误。当我检查 wpengine 上的错误日志时,我发现了以下内容:

PHP 警告:mysqli_query():MySQL 服务器已在 /nas/content/staging/panolam/wp-includes/wp-db.php 第 1931 行消失

它表明某些查询花费了太多时间或其他任何可能的时间。

我在谷歌上寻找解决方案,发现将 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;
}
php wordpress search
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.