从查询中排除某些作者

时间:2012-08-13 作者:Ronin

此脚本列出了用户的最后一篇文章。如何将一些作者排除在guery之外?

<?php

    //List of users sorted descending by date of lastest post written by user
    $uc=array();
    $blogusers = get_users_of_blog();

    if ($blogusers) 
    {
        foreach ($blogusers as $bloguser) 
        {
            $user = new WP_User( $bloguser->user_id );
            if ( !empty( $user->roles ) && is_array( $user->roles ) )
            {
                $userpost = get_posts(\'showposts=1&author=\'.$bloguser->user_id);
                $uc[$bloguser->user_id] = \'\';
                if ($userpost) 
                {
                    $uc[$bloguser->user_id]=$userpost[0]->post_date;
                }
            }
        }
        arsort($uc);
        $i = 0;
        foreach ($uc as $key => $value) 
        {
            $user = get_userdata($key);
            $post_count = count_user_posts($user->ID);
            if ($post_count && $i < 10) 
            {
                $author_posts_url = get_author_posts_url($key);

                //preparing query for titles
                $args=array(
                \'author\' => $user->ID ,
                \'post_type\' => \'post\',
                \'post_status\' => \'publish\',
                \'posts_per_page\' => 1,
                \'ignore_sticky_posts\'=> 1,
                );
                $my_query = null;
                $my_query = new WP_Query($args);

                if( $my_query->have_posts() ) 
                {
                    //echo \'List of Posts for \' . user->user_firstname . \' \' . $user->user_lastname;
                    while ($my_query->have_posts()) : $my_query->the_post(); 

?>

2 个回复
SO网友:pbd

要排除作者,请在用户id前面添加减号。因此,在您的情况下,它可能如下所示:

$args=array(
    \'author\' => \'-2, -3, -4\', //excludes users with id 2, 3 and 4.
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => 1,
    \'ignore_sticky_posts\'=> 1,
);
阅读更多信息the codex

SO网友:amit

嘿,用这个来抓取除ID为-1,2,4的用户之外的所有用户

$blogusers = get_users(array(\'exclude\' => array(1,2,4)));
注意功能get_users_of_blog() 是去润滑的,所以最好使用get_users() 支持的功能excludeing 用户的ID。

结束

相关推荐

为除特定类别之外的所有帖子类型的帖子形成WP_Query

我需要从博客获取帖子,自定义帖子类型1,自定义帖子类型2。自定义职位类型3,来自类别1或类别2或类别3或类别4或类别5或类别6或类别7或类别8。我有这个,但不起作用。我没有得到任何结果,也没有错误。<?php function filter_where_categs() { $where .= \" AND (category_name => array(\'black-star-crescent-moon\', \'audio\', \'b