【问题标题】:Search with only meta Key in Wordpress在 Wordpress 中仅使用元键搜索
【发布时间】:2018-04-27 20:16:04
【问题描述】:

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

在默认的 wordpress 搜索 wordpress 中,在“AND”条件下添加元查询。如果有办法,如果元查询添加或条件也可以。

请帮忙

【问题讨论】:

  • 想想,你首先应该看看WP_Meta_Query。默认关系是AND,但您可以使用ORLIKE 等更改它。如果这不是您的问题,那么请更详细并添加一些示例,您想做什么跨度>
  • 我知道 WP_Meta_Query 它将设置不同元键条件之间的关系。但是在搜索中,当您在默认搜索查询中添加元查询时,查询看起来像 where1=1 和(标题像某物或内容像某物或摘录像某物)和(多个元条件将在此处添加到您的数组)我想要这些与内容、标题和摘录有“或”关系的元条件。
  • look at here。是你想要的吗(只能用WP_User_Query代替WP_Query)?

标签: wordpress


【解决方案1】:

如果您指的是更改搜索结果页面上的 ma​​in 查询(例如 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 搜索 wordpress 中,在“AND”中添加元查询 健康)状况。如果有办法添加元查询或条件,它将 也没事。

挂钩到get_meta_sql 并将AND 替换为OR

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 都很棘手,并且可能与您网站上的插件或修改搜索相关参数/查询的任何自定义代码冲突。甚至是标准查询。因此,使用代码需要您自担风险。

希望对您有所帮助..

【讨论】:

  • 选项#3 基本上不会修改全局$wp_query 对象。但我把这个选项放在列表中是因为如果我是你,我可能会选择那个。
【解决方案2】:

我创建了一个 ajax,我将输入文本作为“search_text”发送,并从存储在 postmeta 中的 meta_key“job_is_featured”值检查文本是否为特征。并检查自定义帖子类型类别 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 );

【讨论】:

  • 我想你对我的问题有点困惑让我再解释一下,默认的 wordpress 搜索查询从标题、摘录和内容中搜索内容或条件(其中标题如关键字或摘录像关键字或内容像关键字)之后,当您添加更多像元这样的参数时,它将在和条件像(其中(标题像关键字或摘录像关键字或内容像关键字)AND(元像关键字或类似关键字的元数据))我想将其更改为 OR
【解决方案3】:

这是你需要在functions.php中添加的代码

此代码适用于 Post Meta 和 Taxonomy 两者

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

【讨论】:

    【解决方案4】:

    在function.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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多