我正在一个会员页面上使用自定义帖子类型和自定义分类法。我的自定义帖子类型称为member,我的自定义分类名为member_groups

我想列出所有成员,但将它们分组到各自的组中。显然,我将35个成员分为9个组–因此,我不想一次执行一次相同的查询,而是希望将它们分组在一起,所以我没有将其进行9次,因此,将Member1,Member4和Member 11分组在一起称为“营销” ”。

我正在使用WP_Query检索帖子类型成员下的所有帖子。我尝试了不同的尝试,但没有成功。

我该如何实现?

#1 楼

因此,您可以考虑自动执行多个查询。

首先,使用get_terms()获得自定义分类法中的术语列表:

<?php
$member_group_terms = get_terms( 'member_group' );
?>


然后,遍历每个循环,每次都运行一个新查询:进行扩展(即,如果您有成百上千的成员或member_group条款,则可能会遇到性能问题)。

评论


是的,它是完美的。我只有一个问题。我想显示诸如这样的文本字段<?php get_post_meta($ member_group_term-> ID,'job_title',true);?>但它没有用。 > ID,但没有任何作用,请您帮忙@Chip Bennett吗?

–Anahit DEV
2015年12月21日在7:53

#2 楼

我找到了一个解决方案,方法是使用自定义查询,然后将其与术语名称进行分组:想要。

但是如果有的话,我仍然对另一种方式感兴趣,也许可以使用Wordpress自己的功能。

评论


我刚刚添加了另一种方法。我倾向于避开任何需要原始SQL查询的内容。

–芯片Bennett
2012年1月25日14:32

我很高兴看到它被标记为正确答案,即使架构在某个时候发生了更改,即使查询在wordpress中停止工作了...在单个查询中将它们全部收集的概念也是正确答案。将分类法​​分组为php的迭代将不会像现在这样规模化。

– wowo_999
13年11月12日23:03



OMG,您是认真编写原始查询吗?可能您只是进入了WP世界,或者从未理会其标准。不应该选择答案。

–pixelngrain
20年7月1日在7:35



#3 楼

甚至更简单:

#4 楼

我有这个确切的需求,Chip的解决方案起作用了,除了一件事:'field' => 'slug'是必需的。显示器是平坦的,因此在这里设置了'get' => 'all'

希望这可以帮助其他人。

#5 楼

$query = new WP_Query( 
   array ( 
      'post_type' => 'member', 
      'orderby'   => 'meta_value', 
      'meta_key'  => 'member_group' 
   ) 
);


然后遍历此查询时,您可以沿这些行使用if
(在php伪代码中)

$groupName = "";
$counter = 0;
if havePosts: while havePosts: thePost

if( $groupName != post->meta_value )
{
if ($counter > 0)
{
</ul>
}
<h1>A group name</h1>
<ul>
<li>member name</li>
}
else
{
<li>member name</li>
}

endwhile;endif

</ul>


我希望能有所帮助。我认为您正在使这一过程变得复杂得多。

更多信息:http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

#6 楼

几年前,我不得不在一个项目上这样做。与djb相似的答案,只是更多细节。这会将所有分类法名称输出为h3,每个帖子标题的项目符号列表链接到其详细信息页面。

<?php // Output all Taxonomies names with their respective items
$terms = get_terms('member_groups');
foreach( $terms as $term ):
?>                          
    <h3><?php echo $term->name; // Print the term name ?></h3>                          
    <ul>
      <?php                         
          $posts = get_posts(array(
            'post_type' => 'member',
            'taxonomy' => $term->taxonomy,
            'term' => $term->slug,                                  
            'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead
          ));
          foreach($posts as $post): // begin cycle through posts of this taxonmy
            setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
      ?>        
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>    
        <?php endforeach; ?>
    </ul>                                                   
<?php endforeach; ?>


#7 楼

好吧,这是一个旧线程,但是如果有人像我一样经过,这可能会有所帮助。
想法是修改主查询,因此我们不需要去模板并生成新的查询和循环...

PS:尚待在大型数据库中进行测试。我的情况令人满意。

function grouped_by_taxonomy_main_query( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) { // Run only on the homepage

        $post_ids = array();

        $terms = get_terms('my_custom_taxonomy');

        foreach ( $terms as $term ) {
            $post_ids = array_merge( $post_ids, get_posts( array( 
                'posts_per_page' => 4, // as you wish...
                'post_type' => 'my_custom_post_type', // If needed... Default is posts
                'fields' => 'ids', // we only want the ids to use later in 'post__in'
                'tax_query' => array( array( 'taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id, )))) // getting posts in the current term
            );
        }

        $query->query_vars['post_type'] = 'my_custom_post_type'; // Again, if needed... Default is posts
        $query->query_vars['posts_per_page'] = 16; // If needed...
        $query->query_vars['post__in'] = $post_ids; // Filtering with the post ids we've obtained above
        $query->query_vars['orderby'] = 'post__in'; // Here we keep the order we generated in the terms loop
        $query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order

    }
}

// Hook my above function to the pre_get_posts action
add_action( 'pre_get_posts', 'grouped_by_taxonomy_main_query' );