0

我正在尝试设置一个 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();?>

希望有人能理解这个混乱,谢谢

4

1 回答 1

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';
}
于 2013-05-09T00:40:11.283 回答