我在使用 get_query_var 时看到的随机字符串是什么?
What is the random string I am seeing when I use get_query_var?
我正在使用 ACF 字段在 wordpress 中设置一个自定义 post 类型的高级搜索页面。
在我的示例中,我在 ?licenseType=Students
中发送 'Students'
我已经注册了我的 query_vars
add_filter( 'query_vars', 'software_register_query_vars' );
function software_register_query_vars( $vars ) {
$vars[] = 'licenseType';
return $vars;
}
并设置我的 pre_get_posts 过滤器
add_action('pre_get_posts','software_pre_get_posts');
function software_pre_get_posts( $query ) {
if ( is_main_query() && $query->get('post_type') == 'uc_software' ) {
$query->set('nopaging','true');
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
$meta_query = array();
if( !empty( get_query_var( 'licenseType' ) ) ){
$meta_query[] = array( 'key' => 'software_license_type', 'value' =>
get_query_var( 'licenseType' ), 'compare' => 'LIKE' );
}
// ...
//I will add more fields here...
// ...
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}
}
}
当我查看正在创建的查询时,我看到
SELECT wp_125_posts.* FROM wp_125_posts INNER JOIN wp_125_postmeta ON (
wp_125_posts.ID = wp_125_postmeta.post_id ) WHERE 1=1 AND ( (
wp_125_postmeta.meta_key = 'software_license_type' AND
wp_125_postmeta.meta_value LIKE
'{4d69cec85576e5c23416d1afb6df73c1a4a288f24aff1da852b18ad70b464309}Students{4d69cec85576e5c23416d1afb6df73c1a4a288f24aff1da852b18ad70b464309}'
) ) AND wp_125_posts.post_type = 'uc_software' AND (wp_125_posts.post_status = 'publish'
OR wp_125_posts.post_status = 'acf-disabled' OR wp_125_posts.post_status = 'private')
GROUP BY wp_125_posts.ID ORDER BY wp_125_posts.post_title ASC
与我的 query_var 一起捕获的随机字符串是什么?有没有办法在没有它的情况下找回我的 query_var?当我使用 $_GET['licenseType'].
时,我看到了同样的事情
我的问题在 wordpress.stackexchange
上得到了回答
"This is a symptom of having a % character in your meta query value. Since 4.8.3 % characters are being escaped (actually replaced by a semi random string) and should be unescaped before executing the sql query."
我还发现"when using a meta_compare value of 'LIKE', WordPress automatically wraps the wildcard character ( % ) around the meta_value string."
我将比较运算符从 LIKE 更改为 =,并删除了随机字符串。
我遇到了同样的情况,但对我来说,从 LIKE
更改为 =
不是一个选择。
对于仍然需要使用 LIKE
的任何人,您可以使用 wpdb::remove_placeholder_escape() 将这些随机字符串转义为 %
。
我正在使用 ACF 字段在 wordpress 中设置一个自定义 post 类型的高级搜索页面。
在我的示例中,我在 ?licenseType=Students
中发送 'Students'我已经注册了我的 query_vars
add_filter( 'query_vars', 'software_register_query_vars' );
function software_register_query_vars( $vars ) {
$vars[] = 'licenseType';
return $vars;
}
并设置我的 pre_get_posts 过滤器
add_action('pre_get_posts','software_pre_get_posts');
function software_pre_get_posts( $query ) {
if ( is_main_query() && $query->get('post_type') == 'uc_software' ) {
$query->set('nopaging','true');
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
$meta_query = array();
if( !empty( get_query_var( 'licenseType' ) ) ){
$meta_query[] = array( 'key' => 'software_license_type', 'value' =>
get_query_var( 'licenseType' ), 'compare' => 'LIKE' );
}
// ...
//I will add more fields here...
// ...
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}
}
}
当我查看正在创建的查询时,我看到
SELECT wp_125_posts.* FROM wp_125_posts INNER JOIN wp_125_postmeta ON (
wp_125_posts.ID = wp_125_postmeta.post_id ) WHERE 1=1 AND ( (
wp_125_postmeta.meta_key = 'software_license_type' AND
wp_125_postmeta.meta_value LIKE
'{4d69cec85576e5c23416d1afb6df73c1a4a288f24aff1da852b18ad70b464309}Students{4d69cec85576e5c23416d1afb6df73c1a4a288f24aff1da852b18ad70b464309}'
) ) AND wp_125_posts.post_type = 'uc_software' AND (wp_125_posts.post_status = 'publish'
OR wp_125_posts.post_status = 'acf-disabled' OR wp_125_posts.post_status = 'private')
GROUP BY wp_125_posts.ID ORDER BY wp_125_posts.post_title ASC
与我的 query_var 一起捕获的随机字符串是什么?有没有办法在没有它的情况下找回我的 query_var?当我使用 $_GET['licenseType'].
时,我看到了同样的事情我的问题在 wordpress.stackexchange
上得到了回答"This is a symptom of having a % character in your meta query value. Since 4.8.3 % characters are being escaped (actually replaced by a semi random string) and should be unescaped before executing the sql query."
我还发现"when using a meta_compare value of 'LIKE', WordPress automatically wraps the wildcard character ( % ) around the meta_value string."
我将比较运算符从 LIKE 更改为 =,并删除了随机字符串。
我遇到了同样的情况,但对我来说,从 LIKE
更改为 =
不是一个选择。
对于仍然需要使用 LIKE
的任何人,您可以使用 wpdb::remove_placeholder_escape() 将这些随机字符串转义为 %
。