ACF关系:先按父类别术语分组帖子,然后按子类别术语分组

时间:2018-01-23 作者:Laurel

我有一个CPTlocations 使用自定义分类法regions 使用层次术语(State是父术语,City是子术语。

Regions Custom Taxonomy

<北卡罗来纳州(家长)夏洛特(孩子)罗利(孩子)乔治亚州(家长)亚特兰大(孩子)使用ACF关系字段,我试图构建一个灵活的内容面板,允许用户选择位置并通过家长词和子词进行分组显示,如:

Display/Result

<北卡罗来纳州夏洛特市温切斯特路位置(邮编)
  • 亚特兰大温切斯特路位置(邮编)
  • 罗利市像这样?我过得很愉快,需要一些帮助。

  • 2 个回复
    最合适的回答,由SO网友:Laurel 整理而成

    经过一段相当长的时间和几个查询/循环等,我确定这可以被清理,或者可能写得更好,但同时这对我来说是可行的!

    <?php
    // Loop through Categories and Display Posts within
    
    // ACF Relationship Field based on CPT \'Locations\' - Post ID
    $locations_field = $panel[\'locations\'];
    
    // CPT \'Locations\'
    $post_type = \'locations\';
    
    // Get all the state taxonomy terms used
    $state_terms = get_terms( array(
        \'taxonomy\' => \'states\',
        \'hide_empty\' => false,
        \'parent\' => 0
    ) );
    
    // Foreach term, loop through Locations posts selected in the relationship field filtered via the state terms. Not actually displaying anything with this query, but creating an array of post IDs.
    foreach( $state_terms as $state_term ) : ?>
        <?php
        $args = array(
            \'post_type\' => $post_type,
            \'posts_per_page\' => -1,  //show all posts
            \'post__in\' => $locations_field,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'states\',
                    \'field\' => \'slug\',
                    \'terms\' => $state_term->slug,
                )
            )
    
        );
        $new_posts = new WP_Query($args);
        $ids = array();
        if( $new_posts->have_posts() ): while( $new_posts->have_posts() ) : $new_posts->the_post(); ?>
            <?php array_push( $ids, get_the_ID() ); ?>
        <?php endwhile; endif; ?>
    
        <?php // Begin to display groups starting with the State term
          if (!empty($ids)) : // CHECK TO MAKE SURE THERE ARE POSTS! ?>
            <h2><?php echo $state_term->name; // State name ?></h2>
            <?php
            // Get all the city taxonomy terms used
            $city_terms = get_terms( array(
                \'taxonomy\' => \'cities\',
                \'hide_empty\' => false,
                \'parent\' => 0
            ) );
            foreach( $city_terms as $city ) : ?>
                <?php
                $args2 = array(
                    \'post_type\' => $post_type,
                    \'posts_per_page\' => -1,  //show all posts
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'cities\',
                            \'field\' => \'slug\',
                            \'terms\' => $city->slug,
                        )
                    ),
                    \'post__in\' => $ids // The array of IDs previously gathered
                );
                $posts2 = new WP_Query($args2);
                if( $posts2->have_posts() ): ?>
                    <div class="location-city">
                        <h2><?php echo $city->name; // City name ?></h2>
                        <?php while( $posts2->have_posts() ) : $posts2->the_post(); ?>
                            <h3><?php  echo get_the_title(); ?></h3>
                        <?php endwhile; ?>
                    </div>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endif; ?>
    
    <?php endforeach; ?>
    

    SO网友:aswinshenoy

    为什么需要关系字段?你不能直接使用regions 与您的CPT进行分类以实现链接?

    现在,按regions 可以使用FacetWP(付费)、Search&;等过滤插件轻松完成;过滤器(免费)。为了对结果进行分组,像FacetWP这样的插件有一个排序功能,可以根据您的分类法对其进行调整以进行分组。否则,如果没有插件,它将是一个复杂而冗长的代码。

    结束

    相关推荐

    ACF关系:先按父类别术语分组帖子,然后按子类别术语分组 - 小码农CODE - 行之有效找到问题解决它

    ACF关系:先按父类别术语分组帖子,然后按子类别术语分组

    时间:2018-01-23 作者:Laurel

    我有一个CPTlocations 使用自定义分类法regions 使用层次术语(State是父术语,City是子术语。

    Regions Custom Taxonomy

    <北卡罗来纳州(家长)夏洛特(孩子)罗利(孩子)乔治亚州(家长)亚特兰大(孩子)使用ACF关系字段,我试图构建一个灵活的内容面板,允许用户选择位置并通过家长词和子词进行分组显示,如:

    Display/Result

    <北卡罗来纳州夏洛特市温切斯特路位置(邮编)
  • 亚特兰大温切斯特路位置(邮编)
  • 罗利市像这样?我过得很愉快,需要一些帮助。

  • 2 个回复
    最合适的回答,由SO网友:Laurel 整理而成

    经过一段相当长的时间和几个查询/循环等,我确定这可以被清理,或者可能写得更好,但同时这对我来说是可行的!

    <?php
    // Loop through Categories and Display Posts within
    
    // ACF Relationship Field based on CPT \'Locations\' - Post ID
    $locations_field = $panel[\'locations\'];
    
    // CPT \'Locations\'
    $post_type = \'locations\';
    
    // Get all the state taxonomy terms used
    $state_terms = get_terms( array(
        \'taxonomy\' => \'states\',
        \'hide_empty\' => false,
        \'parent\' => 0
    ) );
    
    // Foreach term, loop through Locations posts selected in the relationship field filtered via the state terms. Not actually displaying anything with this query, but creating an array of post IDs.
    foreach( $state_terms as $state_term ) : ?>
        <?php
        $args = array(
            \'post_type\' => $post_type,
            \'posts_per_page\' => -1,  //show all posts
            \'post__in\' => $locations_field,
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'states\',
                    \'field\' => \'slug\',
                    \'terms\' => $state_term->slug,
                )
            )
    
        );
        $new_posts = new WP_Query($args);
        $ids = array();
        if( $new_posts->have_posts() ): while( $new_posts->have_posts() ) : $new_posts->the_post(); ?>
            <?php array_push( $ids, get_the_ID() ); ?>
        <?php endwhile; endif; ?>
    
        <?php // Begin to display groups starting with the State term
          if (!empty($ids)) : // CHECK TO MAKE SURE THERE ARE POSTS! ?>
            <h2><?php echo $state_term->name; // State name ?></h2>
            <?php
            // Get all the city taxonomy terms used
            $city_terms = get_terms( array(
                \'taxonomy\' => \'cities\',
                \'hide_empty\' => false,
                \'parent\' => 0
            ) );
            foreach( $city_terms as $city ) : ?>
                <?php
                $args2 = array(
                    \'post_type\' => $post_type,
                    \'posts_per_page\' => -1,  //show all posts
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'cities\',
                            \'field\' => \'slug\',
                            \'terms\' => $city->slug,
                        )
                    ),
                    \'post__in\' => $ids // The array of IDs previously gathered
                );
                $posts2 = new WP_Query($args2);
                if( $posts2->have_posts() ): ?>
                    <div class="location-city">
                        <h2><?php echo $city->name; // City name ?></h2>
                        <?php while( $posts2->have_posts() ) : $posts2->the_post(); ?>
                            <h3><?php  echo get_the_title(); ?></h3>
                        <?php endwhile; ?>
                    </div>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endif; ?>
    
    <?php endforeach; ?>
    

    SO网友:aswinshenoy

    为什么需要关系字段?你不能直接使用regions 与您的CPT进行分类以实现链接?

    现在,按regions 可以使用FacetWP(付费)、Search&;等过滤插件轻松完成;过滤器(免费)。为了对结果进行分组,像FacetWP这样的插件有一个排序功能,可以根据您的分类法对其进行调整以进行分组。否则,如果没有插件,它将是一个复杂而冗长的代码。

    相关推荐