最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - get_search_query() not working with search queries with ampersand in it

programmeradmin0浏览0评论

I am using plain simple code to make a search page

$argscontactdirectory = array(
            "s" => get_search_query(),
            'post_type' => array( 'expertarticle'),
            'orderby' => 'title',
            'order'   => 'ASC',
    );

Above is the code I am using, however, when the search query contains the "&" character it simply doesn't work, if the search query is M&M the URL bar will show ?s=M&M

I tried many things, include urlencode(), but it doesn't work either.

A live example can be seen here: /?s=hello&p_type=is

Any help is appreciated.

I am using plain simple code to make a search page

$argscontactdirectory = array(
            "s" => get_search_query(),
            'post_type' => array( 'expertarticle'),
            'orderby' => 'title',
            'order'   => 'ASC',
    );

Above is the code I am using, however, when the search query contains the "&" character it simply doesn't work, if the search query is M&M the URL bar will show ?s=M&M

I tried many things, include urlencode(), but it doesn't work either.

A live example can be seen here: http://sandbox.indiadairy/?s=hello&p_type=is

Any help is appreciated.

Share Improve this question asked Mar 22, 2019 at 18:34 Deepak KamatDeepak Kamat 1481 silver badge11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

get_search_query escapes the data for outputting to HTML. Can you try the following instead:

$argscontactdirectory = array(
        's' => sanitize_text_field( get_search_query( false ) ),
        'post_type' => array( 'expertarticle'),
        'orderby' => 'title',
        'order'   => 'ASC',
);

Like this?

$argscontactdirectory = urlencode(
array(
        's' => sanitize_text_field( get_search_query( false ) ),
        'post_type' => array( 'expertarticle'),
        'orderby' => 'title',
        'order'   => 'ASC',
)
);
发布评论

评论列表(0)

  1. 暂无评论