我试图在WP\\U查询中搜索包含短语“道尔顿定律”的数据meta_query
. 这是一个AJAX
搜索,下面是我正在做的:
//in this case $_POST[\'query\'] is the phrase "Dalton\'s Law"
$query = apply_filters( \'get_search_query\', $_POST[\'query\'] );
$query = esc_html( $query );
$guide_meta_args = array(
\'post_type\' => array( \'guide\' ),
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => true,
\'posts_per_page\' => 4,
\'meta_query\' => array(
array(
\'key\' => \'guide_raw_data\',
\'value\' => $query,
\'compare\' => \'LIKE\',
),
)
);
我可以成功地键入“道尔顿”,但一旦找到撇号,搜索就失败了。当我从javascript中获取代码时,我可以
console.log
搜索字符串,它是
dalton\\'s
因此,似乎编码工作正常,但是
WP_Query
不喜欢撇号。。。有人有什么建议吗?非常感谢!
最合适的回答,由SO网友:phatskat 整理而成
尝试使用sanitize_text_field 而不是esc_html
wp> $search_query = "Dalton\'s Law <br/>";
string(18) "Dalton\'s Law <br/>"
wp> $query = apply_filters( \'get_search_query\', $search_query );
string(18) "Dalton\'s Law <br/>"
wp> $esc_html = esc_html( $query );
string(29) "Dalton's Law <br/>"
wp> $sanitized = sanitize_text_field( $query );
string(12) "Dalton\'s Law"
esc_html
应用于在输出转换为HTML之前转义输出。