我有以下自定义帖子类型:书籍、文档、示例、指南、在线帮助。它们由以下代码生成:
/*-----------------------------------------------------------------------------------*/
/* CPT - Books */
/*-----------------------------------------------------------------------------------*/
function jeo_cpt_books() {
$labels = array(
\'name\' => _x( \'Books\', \'post type general name\' ),
\'singular_name\' => _x( \'Book\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book\' ),
\'add_new_item\' => __( \'Add New Book\' ),
\'edit_item\' => __( \'Edit Book\' ),
\'new_item\' => __( \'New Book\' ),
\'all_items\' => __( \'All Books\' ),
\'view_item\' => __( \'View Books\' ),
\'search_items\' => __( \'Search Books\' ),
\'not_found\' => __( \'No books found\' ),
\'not_found_in_trash\' => __( \'No books found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Books\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds our books and book specific data\',
\'public\' => true,
\'menu_position\' => 5,
\'menu_icon\' => get_bloginfo(\'template_directory\') . \'/images/icons/icon-book.png\', // Icon Path
\'taxonomies\' => array(\'post_tag\'), // Adds tags to cpt
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
\'has_archive\' => true,
);
register_post_type( \'books\', $args );
}
add_action( \'init\', \'jeo_cpt_books\' );
/* CPT - Books - Taxonomies */
function jeo_cpt_book_tax() {
$labels = array(
\'name\' => _x( \'Book Categories\', \'taxonomy general name\' ),
\'singular_name\' => _x( \'Book Category\', \'taxonomy singular name\' ),
\'search_items\' => __( \'Search Book Categories\' ),
\'all_items\' => __( \'All Book Categories\' ),
\'parent_item\' => __( \'Parent Book Category\' ),
\'parent_item_colon\' => __( \'Parent Book Category:\' ),
\'edit_item\' => __( \'Edit Book Category\' ),
\'update_item\' => __( \'Update Book Category\' ),
\'add_new_item\' => __( \'Add New Book Category\' ),
\'new_item_name\' => __( \'New Book Category\' ),
\'menu_name\' => __( \'Book Categories\' ),
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
);
register_taxonomy( \'book_category\', \'books\', $args );
}
add_action( \'init\', \'jeo_cpt_book_tax\', 0 );
(等等)。。。
我试图在不同页面的页面模板上循环遍历每个CPT,并按“类别”(这是一种分类法)显示它们。以下是我尝试使用的代码,运气不太好:
<?php $args = array(
\'post_type\' => \'books\',
\'book_category\' => get_the_term_list( $post->ID, \'guidebook_category\' ),
\'posts_per_page\' => -1,
\'order\' => \'ASC\',
\'orderby\' => \'ID\'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<h2>Books:</h2>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="resource_masterindex">
<?php the_excerpt(); ?>
</div>
<div class="clear"></div>
<?php endwhile; endif; ?>
<a href="#top" class="backtotop">Back to the top</a>
<?php wp_reset_postdata(); ?>
上面的代码似乎从正确的CPT中获取了所有内容,但我只想显示名为“Step2:Prepare”或“Step3:Find”类别中的帖子。非常感谢您的帮助。
最合适的回答,由SO网友:s_ha_dum 整理而成
我相信你需要tax_query
如下所示from the Codex:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'people\',
\'field\' => \'slug\',
\'terms\' => \'bob\'
)
)
);
$query = new WP_Query( $args );
在您的情况下,这看起来像:
$args = array(
\'post_type\' => \'books\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'book_category\',
\'field\' => \'slug\',
\'terms\' => array(\'step2-prepare\',\'step3-find\')
)
),
\'posts_per_page\' => -1,
\'order\' => \'ASC\',
\'orderby\' => \'ID\'
);
$the_query = new WP_Query( $args );
我在猜测分类法中的slug--“step2-prepare”,“step3-find”--但我认为语法是正确的。未经测试,因为我的数据库中没有您的分类法和帖子。
这应该“仅限于名为“Step2:准备”或“Step3:查找”类别中的帖子”
你不能ORDER BY
类别/分类法--Using wp_query is it possible to orderby taxonomy? 你要做的是循环浏览帖子,创建一个新的排序数组,然后循环浏览以显示帖子。这样做可能会使分页有点奇怪,所以要小心。这是非常粗糙的,没有你的分类法和帖子,很难测试任何东西,除了。。。
$sorted = array();
foreach ($the_query as $tq) {
$terms = wp_get_post_terms( $tq->ID, \'book_category\');
$sorted[$terms[0]->slug] = $tq;
}
然而,这也有问题。首先想到的是,如果你有多个术语,你必须从中选择一个,或者在
$sorted
大堆