请注意,post\\u Type查询变量未设置为标准类型(page、post和attachment,请参阅WP_QUery class file line 2396 on trac), 仅适用于自定义post类型,因此这是您可以针对每个类型进行测试的方式,
add_filter( \'posts_where\' , \'posts_where\', 10, 2);
function posts_where( $args, $wp_query_obj ) {
$type = $wp_query_obj->query_vars[\'post_type\'];
switch(true){
case \'any\'==$type: //query any type (see codex for more details).
break;
case !empty($type) && is_array($type):
//multiple post types define
break;
case !empty($type):
//post type is a custom post.
//this is where you would check for your post type,
if ($type == \'property\') {
$args .= \' AND latitude.meta_key="wp_gp_latitude" \';
$args .= \' AND longitude.meta_key="wp_gp_longitude" \';
}
break;
case $wp_query_obj->is_attachment():
//post type is empty but is a media.
$type=\'attachemnt\';
break;
case $wp_query_obj->is_page():
//post type is empty but is a page.
$type=\'page\';
break;
default:
//post type is empty and not an attachment nor a page, so is a post.
$type=\'post\';
break;
}
return $where;
}
有关WP\\U查询对象及其方法的更多信息,请参阅
codex 页