【问题标题】:Wordpress posts_where?WordPress 帖子_在哪里?
【发布时间】:2013-05-02 15:15:24
【问题描述】:

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

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

【问题讨论】:

    标签: wordpress filter permissions


    【解决方案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';
    }
    

    【讨论】:

    • 好的,所以不要使用 post_where 钩子吗?只是一个带有该条件的简单 WP_Query?
    • 没有一种方法可以实现目标。使用你觉得更舒服的东西。
    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2012-01-27
    • 1970-01-01
    相关资源
    最近更新 更多