任何人都可以解释如何更改以下代码以排除受密码保护的帖子吗?我知道我可以在while语句中使用if语句但是我想从WP_Query中排除它们。
$pq = new WP_Query(array('post_type' => $ptype, 'showposts' => $pshow ));
答案 0 :(得分: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;
}