Answers:
因此,您可以考虑自动执行多个查询。
首先,使用以下命令获取自定义分类法中的术语列表get_terms()
:
<?php
$member_group_terms = get_terms( 'member_group' );
?>
然后,遍历每一个,每次运行一个新查询:
<?php
foreach ( $member_group_terms as $member_group_term ) {
$member_group_query = new WP_Query( array(
'post_type' => 'member',
'tax_query' => array(
array(
'taxonomy' => 'member_group',
'field' => 'slug',
'terms' => array( $member_group_term->slug ),
'operator' => 'IN'
)
)
) );
?>
<h2><?php echo $member_group_term->name; ?></h2>
<ul>
<?php
if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
<li><?php echo the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>
<?php
// Reset things, for good measure
$member_group_query = null;
wp_reset_postdata();
}
?>
尽管这种方法的扩展能力有限,但是我看不到任何特别错误的方法(例如,如果您有成百上千的成员或member_group术语,则可能会遇到性能问题)。
我找到了一个解决方案,方法是使用自定义查询,然后将其与术语名称分组:
SELECT *
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND meta.meta_key = 'active'
AND meta.meta_value = 'active'
AND cat_posts.post_type = 'member'
AND cat_term_taxonomy.taxonomy = 'member_groups'
然后,仅使用常规的foreach查询,便可以提取所需的信息。
但是我仍然对是否存在另一种方式感兴趣,也许是通过使用Wordpress自己的功能。
我有这个确切的需求,Chip的解决方案奏效了,除了一件事:'field' => 'slug'
是必需的。
foreach ( $service_categories as $category ) {
$services = new WP_Query(
array(
'post_type' => 'service',
'tax_query' => array(
array(
'taxonomy' => 'service_category',
'terms' => array( $category->slug ),
'operator' => 'IN',
'get' => 'all',
'field' => 'slug'
)
)
)
); ?>
<h2><?php echo $category->slug; ?></h2>
<?php if ( $services->have_posts() ) { // loop stuff goes here ?>
我还需要使显示的结果平坦,因此'get' => 'all'
在这里设置。
希望这可以帮助其他人。
$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
几年前,我不得不在一个项目上这样做。与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; ?>
好吧,这是一个旧线程,但是如果有人像我一样经过,这可能会有所帮助。这个想法是修改主查询,因此我们不需要去模板并生成新的查询和循环...
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' );