仅显示有帖子的作者

时间:2013-09-11 作者:LBF

我正在使用下面的代码显示所有作者及其帖子(在一个名为“publication”的自定义帖子类型中)。这是可行的,但是它也列出了没有帖子的用户。如何调整此代码以仅显示有帖子的作者?

非常感谢。

    <h1>Articles by Author</h1>

    <?php 
    $allUsers = get_users(\'orderby=title&order=ASC&post_type=publication\');
    $users = array();
    // Remove subscribers from the list as they won\'t write any articles
    foreach($allUsers as $currentUser)
    {
        if(!in_array( \'subscriber\', $currentUser->roles ))
        {
            $users[] = $currentUser;
        }
    }
    ?>

    <?php foreach($users as $user) { ?>

    <div class="authorInfo">
    <h2 class="authorName"><?php echo $user->display_name; ?></h2>

     <?php query_posts( array( \'post_type\' => \'publication\', \'showposts\' => -1, \'author\'=>$user->ID ) ); while (have_posts()) : the_post(); ?>
            <?php the_content(); ?>
            <?php endwhile; ?>
    </div>

    <?php } ?>
更新:使用下面答案中的函数和我昨晚读到的一些东西,下面是我现在所拥有的(仍然没有按照上面的要求工作,但想给你我的最新代码):

<?php 

// count number of Articles (CPT) written by a user
function count_user_posts_by_type( $userid, $post_type = \'publication\' ) {
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type, true, $userid );
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    return $count;


$post_count = count_user_posts_by_type($userid, $post_type);
$allUsers = get_users(\'orderby=title&order=ASC&post_type=publication&role=author\');
$users = array();

// CONDITIONAL -- IS THIS CORRECT?
if ($post_count > 0 ) {

// Remove subscribers from the list as they won\'t write any articles
foreach($allUsers as $currentUser)
{
    if(!in_array( \'subscriber\', $currentUser->roles ))
    {
        $users[] = $currentUser;
    }
}
foreach($users as $user) { ?>


<h2 class="authorName"><?php echo $user->display_name; ?></h2>

 <?php query_posts( array( \'post_type\' => \'publication\', \'showposts\' => -1, \'author\'=>$user->ID, \'orderby\' => \'date\', \'order\' => \'DESC\' ) ); 
 while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
        <?php endwhile; ?>


<?php } } ?>

1 个回复
最合适的回答,由SO网友:golchha21 整理而成

WordPress函数的默认版本提供默认帖子类型“post”的计数,如下所述:

<?php
    $post_count = count_user_posts($userid);
?>
要根据帖子类型检索帖子计数,需要一个自定义函数,如下所示:

<?php
    function count_user_posts_by_type( $userid, $post_type = \'post\' ) {

        global $wpdb;

        $where = get_posts_by_author_sql( $post_type, true, $userid );

        $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

        return $count;
    }
?>
上述功能的使用:

<?php
    $post_count = count_user_posts_by_type($userid, $post_type);
?>
现在,当您获得post计数时,请将代码包装在条件循环中。

如何在功能中使用:

<?php
// count number of Articles (CPT) written by a user
function count_user_posts_by_type( $userid, $post_type = \'publication\' ) {
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type, true, $userid );
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    return $count;
}
$allUsers = get_users(\'orderby=title&order=ASC&post_type=publication\');
$users = array();
// Remove subscribers from the list as they won\'t write any articles
foreach($allUsers as $currentUser)
{
    if(!in_array( \'subscriber\', $currentUser->roles ))
    {
        $users[] = $currentUser;
    }
}

foreach($users as $user) { 

    $post_count = count_user_posts_by_type($user->id);

        if ($post_count > 0 { ?>

        <div class="authorInfo">
        <h2 class="authorName"><?php echo $user->display_name; ?></h2>

         <?php query_posts( array( \'post_type\' => \'publication\', \'showposts\' => -1, \'author\'=>$user->ID ) );
            while (have_posts()) : the_post();
                the_content(); 
            endwhile; ?>
        </div>

    <?php }
} 
?>

结束

相关推荐

User profile update author

因此,我正在尝试创建电子邮件通知给某些用户,以便在更新用户配置文件时对其进行更新。我想向这些用户发送更新的配置文件,以及哪些用户进行了这些更新。有时管理员级别的用户可能会更改其他用户配置文件。我知道我可以连接到profile\\u更新中,在更新配置文件时发送电子邮件,但最大的问题是如何获取有关谁更新了这些配置文件的信息?如有任何关于如何做到这一点的建议,我们将不胜感激。非常感谢。