Wordpress - 从wp_query中排除作者的帖子

时间:2017-09-04 12:16:38

标签: php wordpress

我正在尝试使用ID为“1”的作者从WP_Query中排除作者的帖子。这仍然显示所有用户的所有帖子。

有什么想法吗?

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
    <div class="grid__third">
        ...
    </div>
<?php endwhile; // end of loop
?>

非常感谢!

2 个答案:

答案 0 :(得分:0)

试试这个

global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();?>

// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();}else {
// no posts found
}

答案 1 :(得分:0)

如果您的查询无法使用

$wp_query = new WP_Query( array( 'author' => -1 ) );

然后,您可以尝试其他一些逻辑。就像在循环内部一样,使用POST ID获取作者的ID:

$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here

然后应用if($post_author_id != 1){ // YOUR POST }之类的条件这将显示除ID = 1的作者以外的所有作者的帖子。

完整代码如:

while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
    <div class="grid__third">
        ...
    </div>
<?php
} endwhile; // end of loop

希望这对你有所帮助。感谢。