Get_Terms(),但有其他维度吗?

时间:2019-01-02 作者:Robert Andrews

如何使所有术语匹配:

自定义分类法”company

  • 仅在术语元(ACF字段)中”tags值与给定字符串匹配Publishing“仅当术语有任何自定义类型的帖子时”article“但这些帖子必须只有分类法”format“期限价值”viewpoint“还是它的一个孩子?

    我当前的代码满足上述1)和2)的要求,但我不知道如何添加3)的附加约束article“和4)”format“。。。

      $args = array(
         \'meta_query\' => array(
              array(
                 \'key\'       => \'tags\',
                 \'value\'     => \'Publishing\',
                 \'compare\'   => \'LIKE\'
              )
         ),
         \'taxonomy\'   => \'company\',
         \'hide_empty\' => false,
         \'number\'     => 10,
      );
      $orgs_matched = get_terms($args);
    
      echo \'<ul class="list-group list-unstyled">\';
      foreach ($orgs_matched as $matching_org) {
         echo \'<li class="list-group-item d-flex justify-content-between align-items-center">\';
         echo \'<span><img src="https://www.google.com/s2/favicons?domain=\'.get_field( \'domain\', $matching_org ).\'" class="mr-2"><a href="\' . esc_url( get_term_link( $matching_org ) ) . \'" alt="\' . esc_attr( sprintf( __( \'View all post filed under %s\', \'my_localization_domain\' ), $matching_org->name ) ) . \'">\' . $matching_org->name . \'</a></span>\';
         echo \'<span class="badge badge-primary badge-pill">\'.$matching_org->count.\'</span>\';
         echo \'</li>\';
      }
      echo \'</ul>\';
    
    在我的设置中,“article“帖子可以与以下内容关联:

    • format分类术语,名称值包括viewpoint
    • company分类术语本身有一个ACF复选框字段tags,其中一个值为Publishing“我只想得到”company“具有”tags“的值”Publishing“-但我只对那些有关联的公司感兴趣”article“也有分类法的帖子”format“的值”viewpoint“或更低。

      对于每个结果“company\' term,我想数一数“article“分类法格式的帖子”viewpoint“还有下面。

      更新:我正在考虑下面的方法。基本上,标准get_terms 下面,这是可行的,然后stepping through the resulting array to filter out any terms which don\'t have the term 有疑问

        $args_for_short = array(
           \'taxonomy\'   => \'company\',
           // ACF "tag" checkbox field
           \'meta_query\' => array(
                array(
                   \'key\'       => \'tags\',
                   \'value\'     => \'Publishing\',
                   \'compare\'   => \'LIKE\'
                )
           ),
           \'orderby\'    => \'count\',
           \'order\'      => \'desc\',
           \'hide_empty\' => false,
           \'number\'     => 10,
        );
        // array: actual term objects
        $orgs_list_short = get_terms($args_for_short);
      
        // Strip non-Viewpoints out of the terms array
        $orgs_list_short_filtered = array();
        foreach ($orgs_list_short as $org_short_single) {
          // Get all posts of type \'article\'
          // For each \'article\'
            // Examine associated "format" terms
            // If \'format\' term "viewpoint" or child is not present
              // Remove this term from the term object array
        }
      

  • 2 个回复
    SO网友:Robert Andrews

    我想我已经破解了这个,多亏了Redditor\'s help.该建议实际上是:

    从3和4开始(“使用get\\u posts()构建与条件3和4匹配的post id数组”)

  • 然后按1和2细化(“然后将该数组放入get\\u terms()查询的object\\u ids参数中”)

    // 1. First, get all (4) Viewpoint (taxonomy term) (3) Articles (post type).
    $vp_post_args = array(
      \'post_type\' => \'article\',
      \'tax_query\' => array(
            array(
                \'taxonomy\' => \'format\',
                \'field\' => \'slug\',
                \'terms\' => \'viewpoint\'
            )
        ),
        \'fields\' => \'ids\',
      \'nopaging\'  => \'true\',
    );
    
    // 2b. [Longlist for WP_Query] Then constrain by (1) Company (taxonomy) and its (2) Tag (ACF Checkbox) value.
        $vp_org_term_args_for_posts = array(
            \'object_ids\' => $viewpoint_posts_ints,
            \'taxonomy\'   => \'company\',
            \'meta_query\' => array(
                 array(
                    \'key\'       => \'tags\',
                    \'value\'     => $tag_section,
                    \'compare\'   => \'LIKE\'
                 )
            ),
            \'hide_empty\' => false,
        );
        $viewpoint_org_terms_for_posts = get_terms($vp_org_term_args_for_posts);
    

  • SO网友:Robert Andrews

    此外,我发现这种用例是我需要在我的站点上的多个用例中使用的。

    在某些帖子类型、分类法和元值的名称中进行硬编码会将代码提交给该用例。

    所以我把它移到了一个灵活的函数中。以下是我对社区的第一次积极贡献,也是我能力的提高。也就是说,请随时进一步改进。。。

    起始条件:我选择将分类法“格式”术语“视点”(即起始帖子中的我的维度#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);
    

    相关推荐

    GET_THE_TERMS与wp_GET_POST_TERMS中的奇怪结果

    我正在尝试制作一个面包屑函数,但有一个小问题。。。使用时:$categories = get_the_terms( $post->ID, \'product_cat\' ); 我得到了一个循环中使用的类别数组,等等。唯一的问题是它是按字母顺序排列的。(我希望它按层次顺序排列。)经过一番挖掘,我发现了一种替代方法,即使用wp\\u get\\u post\\u terms(),但我肯定遗漏了一些东西,因为当我使用此方法时:$categories = wp_get_post_terms( $p