簡體   English   中英

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

[英]Wordpress - Exclude posts from author from wp_query

我正在嘗試排除作者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
?>

非常感謝!

嘗試這個

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
}

如果您的查詢無法使用

$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 }帖子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

希望這對您有所幫助。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM