posts_where使用hook只设置一个自定义帖子类型

时间:2015-01-08 10:05:32

标签: php wordpress

在我的wordpress插件中,我使用了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;
} 

1 个答案:

答案 0 :(得分:-1)

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功能, 您的$query函数中会有posts_where个对象,您可以对post_type'属性'设置条件,使其仅影响您的自定义帖子类型。

以下是add_filter函数的WP codex文档的链接: http://codex.wordpress.org/Function_Reference/add_filter

相关问题