如何显示每个用户发布的文章数量

时间:2021-09-27 作者:flexi2202

如何显示每个用户发布的文章数,不包括不发布文章的用户。我的消息类型是recipe。我想在wp QueryTank中收到一个代码,非常感谢您的帮助

我给你一段代码。由于这段代码,我能够显示食谱的总数,但我希望这样:用户A发布了14个烹饪食谱,用户B发布了2个烹饪食谱等。。用户成员1应该有2个配方

<?php
// 1. We define the arguments to define what we want to recover 
$args = array ( \'post_type\' => \'recipe\', \'posts_per_page\' => \'16\', );

// 2. We run the WP Query 
// The Query 
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
 // The Loop 
if ($the_query-> have_posts ()) {
 echo count_user_posts (2, $args); 
echo \'recipes for\'; 
echo get_the_author (2, $args); 
echo \'<br>\'; 
echo count_user_posts (1, $args); 
echo \'recipes for\';
 echo get_the_author (1, $args);

// 3. We launch the loop to display the articles and the authors! 
// The Loop 
echo \'<ul>\'; 
while ($the_query-> have_posts ()) ​{ 
$the_query-> the_post (); 
echo \'<li>\'. get_the_title (). \'<li>\'; 
echo \'<li>\'. get_the_author (). \'<li>\';
​}
​echo \'<ul>\';
​}
​else { 
// no posts found }
/ * Restore original Post Data * / 
wp_reset_postdata ();
​?>

        

    

enter image description here

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

如果您有权访问用户ID,则可以使用count_user_posts() 作用

您将获得如下用户的帖子数量:

//Assume the variable $thisUser is equal to a valid user ID
$ThisUserCount = count_user_posts($thisUser, \'recipe\');
编辑:在调用count\\u user\\u posts()函数的地方,传递的是数组$args,而不是post类型“recipe”。

<?php
// 1. We define the arguments to define what we want to recover
$args = array (
    \'post_type\' => \'recipe\',
    \'posts_per_page\' => \'16\',
);

// 2. We run the WP Query
// The Query
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
// The Loop
if ($the_query-> have_posts()) {
    //Set arguments to grab all authors with published recipes, and order them by user ID
    $authorArgs = array(
        \'orderby\' => \'ID\',
        \'has_published_posts\' => array(\'recipe\'),
    );

    //Create an array of all authors with recipes published
    $recipeAuthors = get_users($authorArgs);

    //Loop through each recipe author
    foreach($recipeAuthors as $user){
        //Output user post count for recipes
        echo count_user_posts($user->ID, \'recipe\');
        echo \' recipes for \';

        //Output user\'s display name
        echo $user->display_name;
        echo \'<br />\';
    }

    // 3. We launch the loop to display the articles and the authors!
    // The Loop
    echo \'<ul>\';
    while ($the_query-> have_posts()) {
        $the_query-> the_post();
        echo \'<li>\'. get_the_title(). \'</li>\';
        echo \'<li>\'. get_the_author(). \'</li>\';
    }
    echo \'</ul>\';
} else {
    // no posts found
}
wp_reset_postdata ();​?>
此外,get\\u the\\u author(2,$args)函数调用不正确。get\\u the\\u author()不再接受任何参数,它只返回循环中当前帖子的作者的显示名称。

SO网友:Pixelsmith

如果您只是想显示在自定义帖子类型中至少发布了一篇文章的作者,您可以忽略上述代码,只需执行以下操作:

// Array of WP_User objects.
$authors = get_users();

// Loop thru the array and get the post count for each user
foreach ( $authors as $author ) {
    $posts = count_user_posts($author->ID, \'movies\');

    // Only return users who have at least one post 
    if ($posts > 0):
        echo \'<p><span>\' . esc_html( $author->display_name ) . \': \' . $posts . \'</span>\';
    endif; 
}

// Only Necessary if you\'re running another loop or query on the page
wp_reset_postdata ();​
祝你好运!

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post