posts_where钩子只设置一种自定义帖子类型


posts_where set only one custom post type using hook

在我的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

相关文章: