开发者

Exclude password protected posts from wordpress page

Can anyone explain how I can change the below co开发者_JAVA技巧de to exclude posts that are password protected? I know I can do so with an if statement within the while statement but I want to exclude them from the WP_Query point.

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


You can make it happen with a post_where filter, just before you execute your query:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜