根据发布的自定义帖子数量,按顺序获取wordpress作者



在我的WordPress v.8.1中,我有一个在自定义帖子songpoem中发布的作者列表。通过下面的代码,我得到了在两个或一个自定义帖子中发布的作者列表。

$authors = get_users(array(
'who' => 'authors',
'has_published_posts' => array('song','poem'),
'orderby' => 'post_count',
'order' => 'DESC',
'number' => '15'
));

下面的代码列出了所有作者及其帖子计数:

foreach ($authors as $user) {
$name = $user->first_name . ' ' . $user->last_name;
$songs = count_user_posts($user->ID, $post_type = "song");
$poems = count_user_posts($user->ID, $post_type = "poem");
echo $name.' has '. $songs .' songs, and '. $poems .' poems;
}

参数中有了'orderby' => 'post_count',我希望首先显示具有最高组合自定义帖子数的作者列表,但它是随机显示的,没有顺序,无论是按post_counts还是按ID

我如何才能订购总帖子最多的作者?

请尝试以下代码。

$authors = get_users(array(
'who' => 'authors',
'has_published_posts' => array('song','poem'),
'orderby' => 'post_count',
'order' => 'DESC',
'number' => '15'
));

创建一个array并计算诗歌+歌曲总数

$author_posts = array();
foreach ($authors as $user) {
$name = $user->first_name . ' ' . $user->last_name;
$songs = count_user_posts($user->ID, $post_type = "song");
$poems = count_user_posts($user->ID, $post_type = "poem");
$author_posts[] = array(
'total' => $songs+$poems,
'label' => $name.' has '. $songs .' songs, and '. $poems .' poems'
);
}

现在使用`usort按总数对数组进行排序。

usort($author_posts, function($a, $b) {
if($a['total']==$b['total']) return 0;
return $a['total'] < $b['total']?1:-1;
}); 

打印输出。

foreach ( $author_posts as $key => $author_post ) {
echo $author_post['label']."</br>";
}

完整的代码

$authors = get_users(array(
'who' => 'authors',
'has_published_posts' => array('song','poem'),
'orderby' => 'post_count',
'order' => 'DESC',
'number' => '15'
));
$author_posts = array();
foreach ($authors as $user) {
$name = $user->first_name . ' ' . $user->last_name;
$songs = count_user_posts($user->ID, $post_type = "song");
$poems = count_user_posts($user->ID, $post_type = "poem");
$author_posts[] = array(
'total' => $songs+$poems,
'label' => $name.' has '. $songs .' songs, and '. $poems .' poems'
);
}
usort($author_posts, function($a, $b) {
if($a['total']==$b['total']) return 0;
return $a['total'] < $b['total']?1:-1;
}); 
foreach ( $author_posts as $key => $author_post ) {
echo $author_post['label']."</br>";
}

经过测试并正常工作

首先,函数get_users((orderby参数与post_count一起工作,但计算所有post类型(这里,它计算poemsong和其他类型,如postpage(。

如果你想按特定的自定义帖子类型对结果进行排序,我建议你使用wpdb类来设计自己的请求。这使您可以通过一个请求获得确切的所需结果,而无需使用foreach对结果进行排序。

我试过这种方法,它奏效了:

$authors = $wpdb->get_results(
"SELECT
$wpdb->users.ID AS author_id,
$wpdb->users.display_name AS author_name,
COUNT($wpdb->posts.ID) AS published_songs_and_poems,
COUNT(CASE WHEN $wpdb->posts.post_type = 'song' THEN $wpdb->posts.ID ELSE NULL END) as published_songs,
COUNT(CASE WHEN $wpdb->posts.post_type = 'poem' THEN $wpdb->posts.ID ELSE NULL END) as published_poems
FROM $wpdb->users
JOIN $wpdb->posts
ON $wpdb->posts.post_author = $wpdb->users.ID
AND $wpdb->posts.post_type IN('song', 'poem') AND $wpdb->posts.post_status = 'publish'
GROUP BY author_id
ORDER BY published_songs_and_poems DESC"
);

此请求返回一个对象,其结果按作者分组,并按已发布歌曲和诗歌的总量排序。

您可以像以前一样使用此对象。类似这样的东西:

array_walk($authors, function($author) {
echo $author->author_name." has published ".$author->published_songs." song(s) and ".$author->published_poems." poem(s).<br/>";
});

我编辑了这个答案,因为第一个版本的重点是如何按合计返回结果post_count。@theKing对此进行了澄清。

您的foreach块中有一个错误,这可能是导致问题的原因。使用这个替代:

foreach ($authors as $user) {
$name = $user->first_name . ' ' . $user->last_name;
$songs = count_user_posts($user->ID, "songs");
echo $name.' has '. $songs .' songs ';
}

简而言之,用简单的"songs"替换$post_type = "songs"。有关可接受参数的详细信息,请参阅文档。

编辑:我误解了这个问题。看看你的查询,一切都是应该的,所以我会在其他地方寻找问题的根源。您是否有可能更改查询的插件或筛选器?你能直接在数据库中复制你的查询吗?它在那里工作吗?(排除表或索引损坏(

试试这个:

<?php wp_list_authors( $args ); ?> 

<?php $args = array(
'post_type'     => 'custom_post_type',
'orderby'       => 'post_count', 
'order'         => 'DESC', 
'number'        => null,
'optioncount'   => false, 
'exclude_admin' => true, 
'show_fullname' => false,
'hide_empty'    => true,
'echo'          => true,
'style'         => 'list',
'html'          => true ); ?> 

我会使用查询监视器插件对此进行攻击。

正如其他人所建议的那样,这可能有几个原因没有达到你预期的效果。一种是您的查询正被另一个插件更改。另一个原因是查询很好,但post_count不是您所期望的。为了弄清事情的真相,你需要确切地了解你的查询是怎么回事。

安装并激活查询监视器插件。加载运行查询的页面,然后打开查询监视器输出。查看查询下方并向下滚动以找到您的查询。这将显示Wordpress正在运行的查询。检查一下,确保它是你所期望的。

如果查询已被更改,则开始停用插件,直到找到罪魁祸首。

如果查询是正确的,那么复制它,转到您的数据库(phpmyadmin或您使用的任何数据库(并自己运行它。您将直接从数据库中看到结果,应该很清楚为什么记录的顺序不是您所期望的。

最新更新