仅搜索产品标题 - WooCommerce

时间:2018-03-19 14:30:30

标签: php wordpress search woocommerce

我有一个搜索功能,目前使用产品文本和所有内容搜索产品等。我希望它只搜索产品标题。我已经使用了这段代码,它已经工作多年了,但已经不复存在了。现在我收到一个错误。为什么呢?

在functions.php中使用的代码

<?php
// Search titles only 
function __search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if(empty($search)) {
        return $search; // skip processing - no search term in query
    }
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array)$q['search_terms'] as $term) {
        $term = esc_sql($wpdb->esc_like($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);

错误

  

警告:参数2到__search_by_title_only()应该是一个参考,在/customers/7/a/4/nemoko.dk/httpd.www/wp-includes/class-wp-hook.php中给出的值在线286

1 个答案:

答案 0 :(得分:2)

posts_search
第二个参数以&amp; &amp; $ wp_query )开头,这意味着它需要将参考变量作为参数。

WP_Query $this过滤了&个对象。只需删除参数

中的 function __search_by_title_only( $search, $wp_query ) 即可

示例:

CustomColor = EEAA99 Gui, +LastFound +AlwaysOnTop -Caption +ToolWindow Gui, Font, s32 Gui, Add, Text, , Some text Gui, Color, %CustomColor% WinSet, TransColor, %CustomColor% 1000 Gui, Show, AutoSize,NoActivate