首页 > 解决方案 > 带有 meta_query 和撇号的 Wordpress Wp_Query 搜索

问题描述

我正在使用名称中包含 ' 的 meta_query 值执行 Wp_Query。例如“角落的”。wp_query 的参数数组看起来像

$location_array = array(
                    'key'     => 'hidden_address',
                    'value'   => stripslashes ( sanitize_text_field( $_GET['adv_location'] ) ),
                    'compare' => 'LIKE',
                    'type'    => 'char',
            );

但是在搜索 "Corner's" 时查询返回 0 个结果。对于“角”返回正确的结果。

标签: wordpress

解决方案


帖子元被保存为角落的 - 所以解决方案是使用 htmlspecialchars

    $value  =   stripslashes ( sanitize_text_field( $_GET['adv_location'] ) );
    $value  =   htmlspecialchars($value,ENT_QUOTES );
    $location_array = array(
                    'key'     => 'hidden_address',
                    'value'   =>  $value,
                    'compare' => 'LIKE',
                    'type'    => 'char',
            );

推荐阅读