从wordpress页面中排除受密码保护的帖子

时间:2011-07-26 02:12:24

标签: wordpress

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

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

1 个答案:

答案 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;
}