此外,我发现这种用例是我需要在我的站点上的多个用例中使用的。
在某些帖子类型、分类法和元值的名称中进行硬编码会将代码提交给该用例。
所以我把它移到了一个灵活的函数中。以下是我对社区的第一次积极贡献,也是我能力的提高。也就是说,请随时进一步改进。。。
起始条件:我选择将分类法“格式”术语“视点”(即起始帖子中的我的维度#4)类型为“文章”的帖子移动到新的帖子类型“视点”。
功能:
/**
 * ==============================================================================
 *                      GET TERMS WITH EXTRA DIMENSIONS
 *  Gets a) Taxonomy Terms that have b) a Meta set and c) are associated with posts
 *  of a particular type.
 *
 *  This is how it works:
 *  1. First, gets IDs of all Posts of specified type.
 *  2. Then, uses that list to constrain getting a) Taxonomy Terms with b) Meta value set.
 *
 *  Arguments:
 *  1. Taxonomy name, 2. Taxonomy Meta Key-and-Value array pair, 3. Post Type name,
 *  4. get_terms \'orderby\', 5. get_terms \'order\', 6. get_terms \'number\'
 *  Example:
 *  $terms_list = get_terms_with_meta_and_posts_of_type(\'company\', array("tags"=>$tax_meta_value), \'viewpoint\', 11);
 *
 *  cf. https://www.reddit.com/r/Wordpress/comments/acmw9r/how_can_i_get_terms_but_with_additional_dimensions/
 *  cf. https://wordpress.stackexchange.com/questions/324441/get-terms-but-with-additional-dimensions
 * ==============================================================================
 */
function get_terms_with_meta_and_posts_of_type($taxonomy, $tax_meta, $post_type, $orderby=\'name\', $order=\'ASC\', $number=\'false\') {
    // 1. Gets ID of all Posts of specified type.
    // Nb. If function calle dmultiple times over, consider calling this posts parts once only,
    // in the callig page. If so, place "global $post_ids;" here to pick it up
    $post_args = array(
      \'post_type\' => $post_type,
      \'fields\' => \'ids\',
      \'nopaging\'  => \'true\',
    );
    $post_ids = get_posts($post_args);
    // 2. Get specified terms associated with those posts
    $term_list_args = array(
        // c) Load only constrained Post IDs in to get_terms():
        \'object_ids\' => $post_ids,
        // a) Only get specified Terms
        \'taxonomy\'   => $taxonomy,
        // b) Only company terms with Tags Checkbox value:
        \'meta_query\' => array(
             array(
                \'key\'       => key($tax_meta),
                \'value\'     => reset($tax_meta),
                \'compare\'   => \'LIKE\'
             )
        ),
        // Order
        \'orderby\'    => $orderby, // want to order by specific post-type count, not all
        \'order\'      => $order,
        \'hide_empty\' => false,
        // Number of terms to show in list
        \'number\'     => $number,
    );
    // Get the terms and return them to whatever called this...
    $terms_list = get_terms($term_list_args);
    return $terms_list;
}
 使用特定的,我这样称呼它。。。
示例1:
      // Get terms with: 1. taxonomy name, 2. taxonomy meta key and value, 3. post type name, 4. orderby, 5. order, 6. number of terms
      $terms_list = get_terms_with_meta_and_posts_of_type(\'company\', array("tags"=>$tax_meta_value), \'viewpoint\', \'count\', \'desc\', 11);
 并且,使用生成的
$terms_list, 我可以循环使用它来输出一个包含这些维度的术语列表。
      // List organisations:
      echo \'<div class="list-group list-unstyled">\';
      foreach ($terms_list as $term_single) {
           echo \'<a href="\' . esc_url( get_term_link( $term_single ) ) . \'" alt="\' . esc_attr( sprintf( __( \'View all post filed under %s\', \'my_localization_domain\' ), $term_single->name ) ) . \'" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">\';
           echo \'<span><img src="https://www.google.com/s2/favicons?domain=\'.get_field( \'domain\', $term_single ).\'" class="mr-2">\' . $term_single->name . \'</span>\';
           echo \'<span class="badge badge-primary badge-pill">\'.get_term_post_count_by_type($term_single,\'company\',\'viewpoint\').\'</span>\';
           echo \'</a>\';
      }
      echo \'</div>\';
 示例2:我不需要输出整个结果的列表,我还可以提供一个简单的总数。。。
$people_terms = get_terms_with_meta_and_posts_of_type(\'person\', \'\', \'viewpoint\');
$count_people = count($people_terms);