【问题标题】:502 bad gateway on customising wordpress search to include ACF custom taxonomies关于自定义 wordpress 搜索以包含 ACF 自定义分类法的 502 bad gateway
【发布时间】:2018-01-11 08:16:01
【问题描述】:

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

您可以在下面找到我在functions.php 中使用的相同代码。

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

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

PHP 警告:mysqli_query(): MySQL server has gone away in /nas/content/staging/panolam/wp-includes/wp-db.php on line 1931

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

我在 google 上寻找解决方案,发现在 my.ini MySQL 设置中将 max_allowed_pa​​cket 更改为 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;
}

【问题讨论】:

  • 这似乎需要做很多工作,只是将分类法添加到搜索中?
  • 这个答案看起来更干净、更容易? :wordpress.stackexchange.com/questions/228645/…
  • 实际上这段代码在我的本地 wamp 服务器上运行良好,但我在登台服务器上遇到了问题。 @Stender 让我试试这个链接上的代码,如果它适合我​​的话。
  • 对不起@Stender,这不符合我的要求。
  • 谁能帮我解决我目前的代码?

标签: wordpress search


【解决方案1】:

我对我的代码做了一些更改,虽然它看起来很多代码但它可以按照我的要求正常工作。

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;
}

【讨论】:

    猜你喜欢
    • 2019-04-11
    • 2023-03-22
    • 2011-03-01
    • 2016-02-28
    • 1970-01-01
    • 2016-06-08
    • 2017-06-02
    • 2015-10-09
    • 1970-01-01
    相关资源
    最近更新 更多