我正在尝试设置一个wordpress网站,该网站使用私人/公共帖子隐藏未登录用户的私人帖子,并允许作者查看所有用户的私人帖子。
我正在尝试使用“posts_where”过滤器进行设置,但无法使其正常工作。
这是我的循环/查询代码,请注意我需要在页面上使用两个过滤私有帖子的循环。我还允许作者能够在functions.php中看到私人帖子
<?php
// filter private posts in user not permitted to view
function privates_control($where) {
if( current_user_can('read_private_posts')) return $where;
global $wpdb;
return " $where AND {$wpdb->posts}.post_status != 'private' "; // or add your
custom status
}
$feature_args = array('post_type' => array( 'post', 'work', 'people', 'events' ),
'posts_per_page' => -1,
'orderby' => 'date', 'order' => 'DSC');
$feature_query = new WP_Query();
add_filter('posts_where', 'privates_control');
$feature_query->query($feature_args);
?>
<?php if ($feature_query->have_posts()) : while ($feature_query->have_posts()) :
$feature_query->the_post(); ?>
<?php the_title(); ?>
<?php endif;?>
<?php wp_reset_query();?>
<?php wp_reset_postdata();?>
<?php
// this is the first loop
$box_args = array('post_type' => array( 'post', 'work', 'people', 'events' ),
'posts_per_page' => -1,
'orderby' => 'date', 'order' => 'DSC');
$boxes_query = new WP_Query();
add_filter('posts_where', 'privates_control');
$boxes_query->query($box_args);
?>
// this is the first loop
<?php if ($boxes_query->have_posts()) : while ($boxes_query->have_posts()) :
$boxes_query->the_post(); ?>
<?php the_title(); ?>
<?php endif;?>
<?php wp_reset_query();?>
<?php wp_reset_postdata();?>
希望有人理解这个烂摊子,谢谢
答案 0 :(得分:1)
您可以在设置后将其添加到$args
(或$feature_args
):
if ( is_user_logged_in() ) {
// this will add argument of showing also private to all logged in users
// or any other condition ou want
$args['post_status'][] = 'private';
}