我正在wordpress中设置一个高级搜索页面,其中包含ACF字段和自定义帖子类型。
在我的示例中,我在url参数中发送“Students”作为?licenseType=学生
*编辑添加PHP代码:
<select id="licenseType" name="licenseType">
<option value="">All License Types</option>
<?php $field = get_field_object(\'software_license_type\');
if( $field )
{
foreach( $field[\'choices\'] as $key => $value )
{
echo \'<option value="\'.$key.\'" \'.((isset($_GET[\'licenseType\']) && $_GET[\'licenseType\'] == $value)?\' selected\':\'\').\' >\'.$value.\'</option>\';
}
}
?></select>
我已注册query\\u vars
add_filter( \'query_vars\', \'software_register_query_vars\' );
function software_register_query_vars( $vars ) {
$vars[] = \'licenseType\';
return $vars;
}
并设置我的pre\\u get\\u 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
随my query\\u var一起捕获的随机字符串是什么?有没有一种方法可以在没有它的情况下检索query\\u var?当我使用$\\u GET[\'licenseType\']时,我看到了同样的情况。