简体   繁体   English

在 wordpress 的管理面板上更改自定义帖子类型的搜索查询

[英]Change search query of a custom post type on the admin panel in wordpress

I want to change the query of search a custom post type in wordpress admin panel.我想更改在 wordpress 管理面板中搜索自定义帖子类型的查询。 I use this method:我使用这种方法:

function change_admin_search( $query ) {

    $post_type = 'custom_post_type';

    if( ! is_admin() )
        return;

    if ( $query->query['post_type'] != $post_type )
        return;

    $search_term = $query->query_vars['s'];
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $num = range(0, 9);
    $persianNumbersOnly = str_replace( $num,$persian, $search_term);
    $englishNumbersOnly = str_replace( $persian, $num, $search_term);
    $query->query_vars['s'] = $englishNumbersOnly;
}

add_action( 'pre_get_posts', 'change_admin_search' );

In this query, it search only with $englishNumbersOnly .在此查询中,它仅使用$englishNumbersOnly进行搜索。 I want to search with $englishNumbersOnly OR $persianNumbersOnly , and $query->query_vars['s'] have a OR in query.我想用$englishNumbersOnly OR $persianNumbersOnly搜索,并且$query->query_vars['s']在查询中有一个OR Actually have a query like this:实际上有这样的查询:

post Like "%$englishNumbersOnly%" OR post Like "%$persianNumbersOnly%"

Thanks谢谢

"I want to show all result ( persian and english )" “我想显示所有结果(波斯语和英语)”

Search term argument of wp_query (ie 's') accepts more than one keyword/term. wp_query 的搜索词参数(即“s”)接受多个关键字/词。 The way you could feed it more than one keyword is to use + sign in the middle of the keywords, and it should be a string not an array.您可以提供多个关键字的方法是在关键字中间使用+号,它应该是字符串而不是数组。 Like this:像这样:

  • $query->set( 's' => 'keywordOne+keywordTwo+keywordThree' )

We could use this to modify your query:我们可以使用它来修改您的查询:

add_action('pre_get_posts', 'change_admin_search');

function change_admin_search($query)
{

    $post_type = 'custom_post_type';

    if (is_admin() && $query->query['post_type'] == $post_type) {
        $search_term = sanitize_text_field($query->query_vars['s']);
        $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
        $num = range(0, 9);
        $persianNumbersOnly = str_replace($num, $persian, $search_term);
        $englishNumbersOnly = str_replace($persian, $num, $search_term);

        $query->set('s',  strval($englishNumbersOnly) . '+' . strval($persianNumbersOnly));
    }
    
}

Note:笔记:

  • I've combined and modified your if statements.我已经合并并修改了您的if语句。
  • I've used sanitize_text_field function on the searched keyword.我在搜索的关键字上使用了sanitize_text_field function。
  • I've used set method for setting the s value.我使用set方法来设置s值。
  • Using strval function is not necessary, but I've used it to make sure you don't get any error(s)/warning(s).没有必要使用strval function,但我用它来确保您不会收到任何错误/警告。

This has not been tested but, theoretically, it should work.这尚未经过测试,但从理论上讲,它应该可以工作。 Let me know if you could get it to work.让我知道你是否可以让它工作。

I found the solution in WP_Query::parse_search method.我在WP_Query::parse_search方法中找到了解决方案。 As @Ruvee says, we can use '+' sign to feed more than one search term for 's' argument, But In parse_search method, the $searchand value is ' AND ' and it ands search_terms.正如@Ruvee 所说,我们可以使用'+'号为's'参数提供多个搜索词,但是在 parse_search 方法中, $searchand的值是' AND '并且它和search_terms。 I Customize parse_search for custom_post_type to use OR instead of AND我为custom_post_type自定义 parse_search 以使用OR而不是AND

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM