列出 WordPress 中作者自定义帖子类型的前 5 个自定义分类法
在我的 WordPress v5.9.1 中,我有自定义帖子类型(歌曲、诗人)和自定义分类(流派)。
在“作者”页面中,我想使用以下代码显示特定作者的两种罐类型的前 5 个分类法:
<?php
$args = array(
'author' => 1,
'post_type' => array('song', 'poem'),
'number' => 5,
'orderby' => 'count', // categories with most posts first
'order' => 'DESC',
'taxonomy' => 'genre'
);
$categories = get_terms($args);
foreach ($categories as $cat) {
echo $cat->name . ' - ' . $cat->count;
}
wp_reset_postdata(); // reset query
?>
上面的代码列出了 5 个随机分类法,而不是给定帖子类型中作者帖子的分类法。
更新 1
考虑到我的目标无法通过上述方法实现,我尝试使用 SQL 查询和 wp_get_object_terms
下面的代码:
global $wpdb;
$SQLquery = "
SELECT $wpdb->posts.ID FROM $wpdb->posts
WHERE $wpdb->posts.post_type IN ('song', 'poem')
AND $wpdb->posts.post_author = $curauth->ID
AND $wpdb->posts.post_status = 'publish'";
$SQLqueryResult = $wpdb->get_results($SQLquery, ARRAY_A); // array of all posts IDs of an author for the post_type's
$SQL_post_ids = wp_list_pluck($SQLqueryResult, 'ID');
$SQL_get_terms = wp_get_object_terms($SQL_post_ids, 'genre');
echo '<ul>';
foreach ($SQL_get_terms as $term) {
echo '<li>' . $term->name . ' - ' . $term->count . '</li>';
}
echo '</ul>';
上面的代码仅按预期结果来自特定作者的术语,但是我无法实现的是:
- 每个
genre
术语的特定作者 ($curauth->ID
) 的帖子总数,预计在echo
中的$term->count
- 按每个术语中最多帖子排序前 5 个术语。
如何才能实现以上两个目标呢?
更新 2
以下是工作代码的预期结果:(
流派名称 [自定义分类法] - 该流派作者的帖子计数 [post_type = 歌曲+诗歌])
- 经典歌曲 - 10 首
- 浪漫歌曲诗歌 - 8
- 虔诚歌曲 - 6
- 民间诗歌 - 3
- 爱国者歌曲 - 2
In my WordPress v5.9.1, I have custom post types (song, poet) and custom taxonomy (genre).
In the Authors page, I wanted to show the top 5 taxonomies from both pots types of a particular author with below code:
<?php
$args = array(
'author' => 1,
'post_type' => array('song', 'poem'),
'number' => 5,
'orderby' => 'count', // categories with most posts first
'order' => 'DESC',
'taxonomy' => 'genre'
);
$categories = get_terms($args);
foreach ($categories as $cat) {
echo $cat->name . ' - ' . $cat->count;
}
wp_reset_postdata(); // reset query
?>
The above code lists 5 random taxonomies than from the authors posts in the given post-types.
Update 1
Considering my objective cannot be achieved with above approach, I have tried below code using SQL Query and than wp_get_object_terms
:
global $wpdb;
$SQLquery = "
SELECT $wpdb->posts.ID FROM $wpdb->posts
WHERE $wpdb->posts.post_type IN ('song', 'poem')
AND $wpdb->posts.post_author = $curauth->ID
AND $wpdb->posts.post_status = 'publish'";
$SQLqueryResult = $wpdb->get_results($SQLquery, ARRAY_A); // array of all posts IDs of an author for the post_type's
$SQL_post_ids = wp_list_pluck($SQLqueryResult, 'ID');
$SQL_get_terms = wp_get_object_terms($SQL_post_ids, 'genre');
echo '<ul>';
foreach ($SQL_get_terms as $term) {
echo '<li>' . $term->name . ' - ' . $term->count . '</li>';
}
echo '</ul>';
The above code results terms only from a particular author as expected, however what I am not able to achieve is:
- Total posts count of particular author (
$curauth->ID
) for eachgenre
term, expected in$term->count
in theecho
- Order the top 5 terms by most posts in each term.
How can I achieve the above two objectives?
Update 2
Below is the expected outcome from the working code:
(Name of the Genre [custom taxonomy] - Posts count [post_type = songs+poem] by an author in that genre)
- Classic songs - 10
- Romantic Poems - 8
- Devotional songs - 6
- Folk poems - 3
- Patriot songs - 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只有两种类型,那么我们最多可以预期两行。此外,您还可以将查询转换为
group by
:If you have only two genres, then we can expect maximum two rows. Also, you can convert your query into a
group by
:您可以做的是计算每个术语的帖子数,然后按帖子数对它们进行排序:
按
term_posts_count
对$terms_with_posts_count_by_author
进行排序,然后打印它。What you can do is to count the post for each term and then sort them by posts count:
Sort the
$terms_with_posts_count_by_author
byterm_posts_count
and then print it.