2011-07-26 30 views
1

任何人都可以解释我如何更改以下代码以排除受密码保护的帖子吗?我知道我可以在while语句中使用if语句,但我想从WP_Query点中排除它们。从wordpress页面排除受密码保护的帖子

$pq = new WP_Query(array('post_type' => $ptype, 'showposts' => $pshow)); 

回答

2

你可以把它用post_where过滤器发生,您执行查询之前:

function getNonPasswordedPosts(){  
    // Add the filter to exclude passworded posts 
    add_filter('posts_where', 'excludePassworded'); 

    // Query for the posts 
    $pq = new WP_Query(array('post_type' => $ptype, 'showposts' => $pshow)); 

    // Remove the filter so that other WP queries don't exclude passworded posts 
    remove_filter('posts_where', 'excludePassworded'); 

    // iterate over returned posts and do fancy stuff  
} 

function excludePassworded($where) { 
    $where .= " AND post_password = '' "; 
    return $where; 
} 
+0

使用remove_filter(“posts_where”你应该也可能删除查询过滤器后,“excludePassworded” ); – HWD

+0

良好的致电@HWD - 我已经更新了我的答案。 – Pat