查询自定义帖子类型和按自定义帖子类型排序 [英] Query Custom Post Types & Order By Custom Post Type
本文介绍了查询自定义帖子类型和按自定义帖子类型排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以查询多个CPT,然后以预设方式订购?
Is it possible to query multiple CPTs and then order in a preset way?
例如,我有2个CPT& 1是WP默认的发布,并希望循环返回6个结果,顺序如下。
For example, I have 2 CPTs & 1 being the WP default 'post' and would like the loop to return 6 results, ordering as below.
- CPT-1
- CPT-2
- POST
- CPT-1
- CPT-2
- POST
- CPT-1
- CPT-2
- POST
- CPT-1
- CPT-2
- POST
是否可以不拆分循环?
我确实进行了快速搜索,但只能找到一篇文章与此相关的是,似乎无法再使用的解决方案...
I did have a quick search, but could only find one article relating to this, the solutions from which don't appear to work any more...
推荐答案
此处是Sephsekla代码的简化版本:
Here is a shortened version of Sephsekla's code:
$my_post_types = array( 'CPT-1', 'CPT-2', 'post', 'CPT-1', 'CPT-2', 'post' );
$posts_shown = array();
$args = array(
'post_type' => array( 'CPT-1', 'CPT-2', 'post' ),
'post_status' => 'publish',
'posts_per_page' => -1
);
$my_query = new WP_Query( $args );
foreach ( $my_post_types as $post_type ):
while ( $my_query->have_posts() ): $my_query->the_post();
if ( $post_type == get_post_type() && ! in_array( get_the_ID(), $posts_shown ) ) {
echo '<pre>' . get_post_type() .': '. get_the_title() . '</pre>';
$posts_shown[] = get_the_id();
break;
}
endwhile;
$my_query->rewind_posts();
endforeach;
wp_reset_postdata();
这篇关于查询自定义帖子类型和按自定义帖子类型排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文