在WordPress中仅使用元密钥搜索



我需要帮助来自定义默认的WordPress搜索。我不想在WordPress搜索中包括标题,内容或摘录,但想根据该帖子的元值进行搜索。

在默认的WordPress搜索中,WordPress在"one_answers"条件中添加元查询。如果有一种方法,如果元查询添加或条件也可以。

请帮助

如果您指的是在搜索结果页面上更改 MAIM 查询(例如http://example.com/?s=hello(,请尝试以下一个选项:

选项#1

这是:

我不想在WordPress中包括标题或内容或摘录 搜索

挂钩到pre_get_posts并为s参数清空。

add_action( 'pre_get_posts', function( $wp_query ){
    if ( $wp_query->is_main_query() && $wp_query->is_search() ) {
        $wp_query->set( 's', '' );
    }
});

选项#2

这是:

在默认的WordPress搜索中 健康(状况。如果有一种方法,如果元查询添加或条件,它将 也可以。

挂钩到get_meta_sql,然后用AND代替CC_5。

add_filter( 'get_meta_sql', function( $sql, $meta_query, $type, $primary_table ){
    global $wpdb;
    if (
        'post' === $type &&
        $wpdb->posts === $primary_table &&
        is_main_query() && is_search()
    ) {
        if ( $sql['where'] && ' AND (' === substr( $sql['where'], 0, 6 ) ) {
            $sql['where'] = ' OR ' . substr( $sql['where'], 5 );
        }
    }
    return $sql;
}, 10, 4 );

选项#3

创建一个自定义搜索页面并使用自定义WP_Query实例查询帖子。

$q = new WP_Query([
    'meta_key'   => 'some_key',
    'meta_value' => 'some_value',
    // Or use meta_query
]);

选项#1和#2都是棘手的,并且可能与网站上的插件或任何自定义代码进行了冲突,或者修改与搜索相关的参数/查询的任何自定义代码。甚至是标准查询。因此,请自己使用代码。

希望有帮助..

i从其中创建一个ajax,从其中我将输入文本发送为" search_text",并检查文本是元素是否来自meta_key" job_is_featured" job_is_featured"值,该值存储在postmeta中。还要签到自定义邮政类型类别ID。

     $catids = '22';
  $args = array(
'post_type' => 'job', 'order' => 'DESC','posts_per_page' => '-1','s' => 'search_text' ,'orderby' => 'meta_value',
'tax_query' => array(
    array(
        'taxonomy' => 'job-category',
        'field'    => 'term_id',
        'terms' => $catids,
    ),
   ),
   'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
    array(
        'key' => 'job_is_featured',
        'value'    => 1,
        'compare' => '=',
    ),
    array(
        'key' => 'job_is_featured',
        'value'    => 0,
        'compare' => '=',
    ),
),
    array(
        'key' => 'job_location',
        'value'    => 'search_text',
        'compare' => 'LIKE',
    ),
),
 );
$the_query = new WP_Query( $args );

这是您需要添加functions.php

的代码

此代码将与后元和分类法一起使用

if(!function_exists('custom_wp_meta_and_tax_search')){
    function custom_wp_meta_and_tax_search($search_args){
        /* taxonomy query and meta query arrays */
        $tax_query = array();
        $meta_query = array();
        /* Keyword Based Search */
        if( isset ( $_GET['keyword'] ) ) {
            $keyword = trim( $_GET['keyword'] );
            if ( ! empty( $keyword ) ) {
                $search_args['s'] = $keyword;
            }
        }
        /* Custom Meta Key Parameter */ 
        if( isset($_GET['meta_input']) && ($_GET['meta_input'] != 'any')){
            $meta_query[] = array(
                'key' => 'custom_meta_key',
                'value' => $_GET['meta_input'],
                'compare' => 'like',                    
            );
        }
        $min_value = '';
        $max_value = '';
        if(isset($_GET['min-value']) && $_GET['min-value'] <> 'any'){
            $min_value = $_GET['min-value'];
        }
        if(isset($_GET['max-value']) && $_GET['max-value'] <> 'any'){
            $max_value = $_GET['max-value'];
        }
        /* Logic for Min and Max value Parameters */
        /* You need to replace custom_tax with custom taxonomy */
        if( isset($min_value) && ($min_value != 'any') && isset($max_value) && ($max_value != 'any') ){
            $min_value = doubleval($min_value);
            $max_value = doubleval($max_value);
            if( $min_value >= 0 && $max_value > $min_value ){
                $meta_query[] = array(
                    'key' => 'custom_tax',
                    'value' => array( $min_value, $max_value ),
                    'type' => 'NUMERIC',
                    'compare' => 'BETWEEN'
                );
            }
        }else if( isset($min_value) && ($min_value != 'any') ){
            $min_value = doubleval($min_value);
            if( $min_value > 0 ){
                $meta_query[] = array(
                    'key' => 'custom_tax',
                    'value' => $min_value,
                    'type' => 'NUMERIC',
                    'compare' => '>='
                );
            }
        }else if( isset($max_value) && ($max_value != 'any') ){
            $max_value = doubleval($max_value);
            if( $max_value > 0 ){
                $meta_query[] = array(
                    'key' => 'custom_tax',
                    'value' => $max_value,
                    'type' => 'NUMERIC',
                    'compare' => '<='
                );
            }
        }
        // /* if more than one taxonomies exist then specify the relation */
        $tax_count = count( $tax_query );
        if( $tax_count > 1 ){
            $tax_query['relation'] = 'AND';
        }
        /* if more than one meta query elements exist then specify the relation */
        $meta_count = count( $meta_query );
        if( $meta_count > 1 ){
            $meta_query['relation'] = 'AND';
        }
        if( $tax_count > 0 ){
            $search_args['tax_query'] = $tax_query;
        }
        /* if meta query has some values then add it to base home page query */
        $search_args['meta_query'] = $meta_query;

        /* Sort By meta value */
        /* You need to replace "custom_meta_key" with the key of custom field you created with post. */
        if( (isset($min_value) && ($min_value != 'any')) || ( isset($max_value) && ($max_value != 'any') ) ){
            $search_args['orderby'] = 'meta_value_num';
            $search_args['meta_key'] = 'custom_meta_key';
            $search_args['order'] = 'ASC';
        }

        return $search_args;
    }
}
add_filter('custom_search_parameters','custom_wp_meta_and_tax_search');

现在运行查询并使用元元和分类法

过滤您的帖子
$custom_search_args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'paged' => $paged,  
);
$custom_search_args = apply_filters('custom_search_parameters',$custom_search_args);
$search_query = new WP_Query( $custom_search_args );

添加此函数.php

                add_action( 'pre_get_posts', function( $q )
                {
                if( $title = $q->get( '_meta_or_title' ) )
                {
                add_filter( 'get_meta_sql', function( $sql ) use ( $title )
                {
                global $wpdb;
                // Only run once:
                static $nr = 0; 
                if( 0 != $nr++ ) return $sql;
                // Modified WHERE
                $sql['where'] = sprintf(
                " AND ( %s OR %s ) ",
                $wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $title),
                mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
                );
                return $sql;
                });
                }
                });

用法

                $meta_query = array();
                $args = array();
                $search_string = "test";
                $meta_query[] = array(
                'key' => 'staff_name',
                'value' => $search_string,
                'compare' => 'LIKE'
                );
                $meta_query[] = array(
                'key' => 'staff_email',
                'value' => $search_string,
                'compare' => 'LIKE'
                );
                //if there is more than one meta query 'or' them
                if(count($meta_query) > 1) {
                $meta_query['relation'] = 'OR';
                }
                // The Query
                $args['post_type'] = "staff";
                $args['_meta_or_title'] = $search_string; //not using 's' anymore
                $args['meta_query'] = $meta_query;

                $the_query = new WP_Query($args)

if($ query-&gt; is_search((({

$meta_query = $query->get( 'meta_query' );
//print_r($meta_query);
$meta_query[] = array('key'       => '_product_attributes', /* Product Attribute Meta key Here example (_sku, _stock, _stock_status, height, width ) */
    'value'     => $city,
    'compare'   => 'LIKE');
$query->set( 'meta_query', $meta_query );

}

}

}

add_action('woocommerce_product_query','add_custom_search2'(;

最新更新