我想显示每个分类中的标签,以及每个标签的总帖子计数,并按每个标签中的帖子数量排序.

如果我有类别术语"体育",我想显示所有的标签与帖子计数包括在体育类别的帖子,如板球(50),足球(40),羽毛球(5)

因为我知道我必须检索当前术语中的所有帖子,这很简单,我可以通过

$args = array(
    'tax_query' => array(
      array(
        'taxonomy' => $term->taxonomy,
        'field' => 'slug',
        'terms' => $term->slug,
      ),
    ),
    'fields' => 'ids', // Just return post IDs
  );
  $query = new WP_Query($args);

然后我必须得到所有可以通过以下方式完成的帖子标签:

foreach ($query->posts as $post_id) {
    $post_tags = get_the_tags($post_id); // Get tags for each post
    if ($post_tags) {
      foreach ($post_tags as $tag) {
        $tags[$tag->term_id] = $tag->name; // Add tag IDs and names to array
      }
    }
  }

我想要做的是计算在每个标签中有多少帖子,然后按标签中的帖子总数排序,并返回20个标签,也尽可能优化

推荐答案

我希望这就是你所要求的

function display_tags(){
    
    if (is_tag()) return;   // Return if term is Tag
    $current_term_id = get_queried_object_id(); // Get the current term ID

    // fetch post IDs within the current term and their tag counts
    $query_args = array(
        'posts_per_page' => -1, // get all posts
        'tax_query' => array(
            array(
                'taxonomy' => get_term($current_term_id)->taxonomy, // current taxonomy
                'field' => 'term_id',
                'terms' => $current_term_id,
            ),
        ),
        'fields' => 'ids', // Fetch only post IDs
        'post_status' => 'publish', // get only published posts
        'update_post_meta_cache' => false, // Optimize by avoiding unnecessary meta retrieval
        'update_term_meta_cache' => false, // same here
    );
    $posts_and_tags = get_posts($query_args);

    // organize tags and their post counts efficiently
    $tag_counts = array();
    foreach ($posts_and_tags as $post_id) {
        $post_tags = wp_get_post_tags($post_id, array('fields' => 'ids')); // fetch only tag IDs
        foreach ($post_tags as $tag_id) {
            $tag_counts[$tag_id] = isset($tag_counts[$tag_id]) ? $tag_counts[$tag_id] + 1 : 1;
        }
    }

    // sort tags by their total posts in descending order
    arsort($tag_counts);
    
    // extract top 20 tags as an array
    $top_tags = array_slice($tag_counts, 0, 20, true);

    // access and return the sorted tags
    $data = '';
    foreach ($top_tags as $tag_id => $count) {
        $data .= '<a href="' . get_tag_link($tag_id) . '" class="tags_with_count">' . get_term($tag_id)->name . ' (' . $count . ')</a>';
    }
    return $data;
}

Php相关问答推荐

Postgrid Webhook不叫laravel

从WooCommerce邮箱通知中的订单详细信息中删除产品列表

为什么调用干预\映像引发错误驱动程序\GD\Driver not found?

向在添加到购物车上动态创建的WooCommerce产品添加发货类别

有没有办法为整个PHP应用程序启用严格类型

从订单项目中获取ACF WooCommerce产品价值

Chillerlan/php-iOS名称字段的QR vCard错误

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

获取作者';在WordPress中使用PHP

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

如何在 PHP 中对具有相同数组值的值求和

在 PHP 中验证签名

PHP preg_replace括号和方括号内的所有逗号

如何用 php ML 解决这个问题?

为什么在 phpunit 的开头测试 PHP_VERSION?

遇到特定键时修改二维数组以创建嵌套数据集

Laravel 10 单元测试看不到来自 Artisan Command 的数据库更改

Symfony 6 中的入口点 (public/index.php) - 它是如何工作的?

使用 PHP 有条件地使用关键字 Twilio Programmable SMS