列出 WordPress 中作者自定义帖子类型的前 5 个自定义分类法

发布于 2025-01-12 08:03:41 字数 2056 浏览 0 评论 0原文

在我的 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>';

上面的代码仅按预期结果来自特定作者的术语,但是我无法实现的是:

  1. 每个 genre 术语的特定作者 ($curauth->ID) 的帖子总数,预计在echo 中的 $term->count
  2. 按每个术语中最多帖子排序前 5 个术语。

如何才能实现以上两个目标呢?

更新 2

以下是工作代码的预期结果:(

流派名称 [自定义分类法] - 该流派作者的帖子计数 [post_type = 歌曲+诗歌])

  1. 经典歌曲 - 10 首
  2. 浪漫歌曲诗歌 - 8
  3. 虔诚歌曲 - 6
  4. 民间诗歌 - 3
  5. 爱国者歌曲 - 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:

  1. Total posts count of particular author ($curauth->ID) for each genre term, expected in $term->count in the echo
  2. 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)

  1. Classic songs - 10
  2. Romantic Poems - 8
  3. Devotional songs - 6
  4. Folk poems - 3
  5. Patriot songs - 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2025-01-19 08:03:41

如果您只有两种类型,那么我们最多可以预期两行。此外,您还可以将查询转换为group by

$SQLquery = "
SELECT count($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'
group by $wpdb->posts.post_type
order by count(*) desc;";

If you have only two genres, then we can expect maximum two rows. Also, you can convert your query into a group by:

$SQLquery = "
SELECT count($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'
group by $wpdb->posts.post_type
order by count(*) desc;";
梦情居士 2025-01-19 08:03:41

您可以做的是计算每个术语的帖子数,然后按帖子数对它们进行排序:

$terms_with_posts_count_by_author = array();
foreach ($SQL_get_terms as $term) {
    $args = array(
         'nopaging' => true,
         'author' => $curauth->ID,
         'post_type' => array('song', 'poem'),
         'tax_query' => array(
            array(
                'taxonomy' => 'genre',
                'field' => 'slug',
                'terms' => $term->slug
            ),
        ),
        'fields' => 'ids'
    );
    $posts_by_term = get_posts( $args );
    
    $terms_with_posts_count_by_author[] = array('term_name' => $term->name, 'term_posts_count' => count( $posts_by_term ));
}

term_posts_count$terms_with_posts_count_by_author 进行排序,然后打印它。

usort($terms_with_posts_count_by_author, function ($a, $b) { return ($a["term_posts_count"] < $b["term_posts_count"]) ? 1 : -1; });

echo '<ul>';
foreach( $terms_with_posts_count_by_author as $term_with_post_count ){
    echo '<li>' . $term_with_post_count["term_name"] . ' - ' . $term_with_post_count["term_posts_count"] . '</li>';
}
echo '</ul>';

What you can do is to count the post for each term and then sort them by posts count:

$terms_with_posts_count_by_author = array();
foreach ($SQL_get_terms as $term) {
    $args = array(
         'nopaging' => true,
         'author' => $curauth->ID,
         'post_type' => array('song', 'poem'),
         'tax_query' => array(
            array(
                'taxonomy' => 'genre',
                'field' => 'slug',
                'terms' => $term->slug
            ),
        ),
        'fields' => 'ids'
    );
    $posts_by_term = get_posts( $args );
    
    $terms_with_posts_count_by_author[] = array('term_name' => $term->name, 'term_posts_count' => count( $posts_by_term ));
}

Sort the $terms_with_posts_count_by_author by term_posts_count and then print it.

usort($terms_with_posts_count_by_author, function ($a, $b) { return ($a["term_posts_count"] < $b["term_posts_count"]) ? 1 : -1; });

echo '<ul>';
foreach( $terms_with_posts_count_by_author as $term_with_post_count ){
    echo '<li>' . $term_with_post_count["term_name"] . ' - ' . $term_with_post_count["term_posts_count"] . '</li>';
}
echo '</ul>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文