我在codex:中发现了这个
// Filter to hide protected posts
function exclude_protected($where) {
global $wpdb;
return $where .= " AND {$wpdb->posts}.post_password = '' ";
}
// Decide where to display them
function exclude_protected_action($query) {
if( !is_single() && !is_page() && !is_admin() ) {
add_filter( 'posts_where', 'exclude_protected' );
}
}
// Action to queue the filter at the right time
add_action('pre_get_posts', 'exclude_protected_action');
但它没有起作用。也许是因为我定制了我的循环。我希望做一些简单的事情,沿着这些路线:
<?php $postsLatest = get_posts('numberposts=10&passwordproteced=false'); foreach($postsLatest as $post) { ?>
这可能吗?
WordPress 3.9中的has_password
和post_password
参数+
您写道:
我希望做一些简单的事情,沿着这些路线:
passwordproteced=false
您非常接近,因为您可以使用布尔has_password
参数:
$postsLatest = get_posts( 'has_password=false' );
获取没有密码的帖子。
您可以在WP_Query()
的Codex中阅读更多关于此参数的信息,因为get_posts
只是WP_Query()
的包装器,并且共享输入参数。
注意这只适用于WordPress 3.9+
:
has_password(bool)-对于有密码的帖子为true;帖子为false没有密码;null适用于所有有密码和无密码的帖子(3.9版可用)。
还有另一个可能值的新post_password
参数:
- null(忽略)
- false(仅发布没有密码的帖子)
- true(仅使用密码发布)
- 字符串(使用此确切密码发布)
您可以查看此Trac票证。
将pre_get_posts
筛选器与get_posts一起使用:
使用pre_get_posts
过滤器失败的原因是,默认情况下get_posts()
会抑制所有pre_get_posts
过滤器。使用suppress_filters=false
参数进行应用。
您可以在自定义循环中执行此操作。
<?php
if ( post_password_required() ) {
continue;
} else {
echo 'normal post';
}
?>