php
  • wordpress
  • 2015-01-08 37 views 0 likes 
    0

    只设置一个自定义帖子类型我使用post_where筛选器,但它影响我网站的所有其他帖子类型。posts_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

    有你自己尝试新鲜事物?如果是这样,请提供您有问题的代码,而且您的问题/问题还不清楚。 – Epodax

    回答

    -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 = 2add_filter功能, 你将有一个$query对象在posts_where功能你可以在你的post_type上添加一个条件,使其只对你的自定义帖子类型有影响。

    这里是为add_filter功能链接到WP抄本文档: http://codex.wordpress.org/Function_Reference/add_filter

    +0

    这种情况只能在后端工作,而不能在前端获得帖子类型 –

    相关问题