将自定义元数据添加到wordpress搜索中

时间:2014-06-10 11:32:47

标签: php wordpress

我目前正在尝试将自定义元素值的主题添加到搜索查询中,但它并不适用于我。例如, _My_meta_value_key3 是产品代码,但如果我尝试搜索它,则wp找不到匹配项。我真的想在没有任何插件的情况下做到这一点,所以欢迎任何建议。此外,此代码目前位于主题函数中。

function My_custom_search_query( $query ) {
if ($query->is_search()) {
    $query->set('meta_query', array(
        array(
        'key' => '_My_meta_value_key',
        'value' => $query->query_vars['s'],
        'compare' => 'LIKE'
        ),
        array(
        'key' => '_My_meta_value_key2',
        'value' => $query->query_vars['s'],
        'compare' => 'LIKE'
        ),
        array(
        'key' => '_My_meta_value_key3',
        'value' => $query->query_vars['s'],
        'compare' => 'LIKE'
        )
    ));
    return $query;
};}

add_filter( 'pre_get_posts', 'My_custom_search_query');

2 个答案:

答案 0 :(得分:1)

我已经花了一个小时来寻找解决方案,但没有任何解决方法,因此我不得不自己编写解决方案。它已经过测试,并且可以与Wordpress 5.3.1一起使用,并且是解决此问题的唯一解决方案,我可以在Internet上找到它。

将以下代码放入您的functions.php文件中,以挂接到posts_clauses过滤器中。

/**
 * Include meta fields in search
 * 
 * @author Mindaugas // meevly.com
 * @link https://meevly.com/services/custom-wordpress-themes-and-plugins/
 * 
 * @param array $pieces query pieces.
 * @param WP_Query $args query object.
 * @return array
 */
function mv_meta_in_search_query( $pieces, $args ) {
    global $wpdb;

    if ( ! empty( $args->query['s'] ) ) { // only run on search query.
        $keywords        = explode(' ', get_query_var('s'));
        $escaped_percent = $wpdb->placeholder_escape(); // WordPress escapes "%" since 4.8.3 so we can't use percent character directly.
        $query           = "";

        foreach ($keywords as $word) {
            $query .= " (unique_postmeta_selector.meta_value LIKE '{$escaped_percent}{$word}{$escaped_percent}') OR ";
        }

        if ( ! empty( $query ) ) { // append necessary WHERE and JOIN options.
            $pieces['where'] = str_replace( "((({$wpdb->posts}.post_title LIKE '{$escaped_percent}", "( {$query} (({$wpdb->posts}.post_title LIKE '{$escaped_percent}", $pieces['where'] );
            $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS unique_postmeta_selector ON ({$wpdb->posts}.ID = unique_postmeta_selector.post_id) ";
        }
    }

    return $pieces;
}
add_filter( 'posts_clauses', 'mv_meta_in_search_query', 20, 2 );

您的查询应如下所示。 请注意,suppress_filters => false是强制性的!没有它将无法工作。

$search_posts_array = array(
    'suppress_filters' => false,
    's'                => $keyword,
);

$search_results = get_posts( $search_posts_array );

如果我为您节省了很多时间,请不要犹豫! :)

答案 1 :(得分:0)

找到此http://wpdevsnippets.com/extend-search-include-custom-fields-without-plugin/

function custom_search_where($pieces) {

// filter to select search query
if (is_search() && !is_admin()) {

    global $wpdb;
    $custom_fields = array('field1','field2'); // Array for custom meta fields
    $keywords = explode(' ', get_query_var('s'));
    $query = "";
    foreach ($custom_fields as $field) {
         foreach ($keywords as $word) {
             $query .= "((mypm1.meta_key = '".$field."')";
             $query .= " AND (mypm1.meta_value  LIKE '%{$word}%')) OR ";
         }
    }

    if (!empty($query)) {
        // add to where clause
        $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);

        $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
    }
}
return ($pieces);
}

add_filter('posts_clauses', 'custom_search_where', 20, 1);