在我的WordPress Pulgin中,我使用了post_where
过滤器,但它会影响我网站的所有其他帖子类型。
我的问题是如何仅为"property"
帖子设置此过滤器
add_filter('posts_where', 'posts_where');
function posts_where($where)
{
global $wpdb,$wp_query;
$where .= ' AND latitude.meta_key="wp_gp_latitude" ';
$where .= ' AND longitude.meta_key="wp_gp_longitude" ';
return $where;
}
add_filter( 'posts_where' , 'posts_where', 10, 2);
function posts_where( $where, $query ) {
global $wpdb,$wp_query;
if ($query->query_vars['post_type'] == 'property') {
$where .= ' AND latitude.meta_key="wp_gp_latitude" ';
$where .= ' AND longitude.meta_key="wp_gp_longitude" ';
}
return $where;
}
如果将$priority = 10 //normal
和$accepted_args = 2
添加到add_filter
函数中,您将在posts_where
函数中有一个$query
对象,您可以在post_type
上为"属性"设置条件,使其仅影响您的自定义帖子类型。
以下是add_filter
函数的WP法典文档的链接:http://codex.wordpress.org/Function_Reference/add_filter