我有一个名为“services”的自定义帖子类型,其中包含一个名为“location”的自定义分类。每个帖子分为“位置A”或“位置B”。
我需要查询“服务”帖子类型中的所有帖子,并按日期排序。但是,我不想按日期列出,而是按每个类别替换帖子,如下所示:
是否可以修改以下查询来实现此目的?或者我是否必须进行一些高级数据库加入/合并(不熟悉该方法)?
<?php $args = array(
'post_type' => 'services',
'posts_per_page' => '-1',
'order_by' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array ('location-a', 'location-b')
)
)
);
$query = new WP_Query( $args );
if (have_posts()) : while( $query->have_posts() ) : $query->the_post(); ?>
附加说明:
这可能吗?非常感谢。
答案 0 :(得分:1)
请检查代码是否适合您。对不起,代码太长了
<?php
$a1 = get_posts(array(
'post_type' => 'services',
'posts_per_page' => '-1',
'order_by' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'location-a'
)
),
'fields' => 'ids' // only get post IDs.
));
$a2 = get_posts(array(
'post_type' => 'services',
'posts_per_page' => '-1',
'order_by' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => 'location-b'
)
),
'fields' => 'ids' // only get post IDs.
));
if (count($a1) >= count($a2)) {
$count = count($a1);
} else {
$count = count($a2);
}
$newArray = array();
for ($i = 0; $i < $count; $i++) {
$newArray[] = $a1[$i];
$newArray[] = $a2[$i];
}
$wp_query = new WP_Query(array(
'post_type' => 'product',
'post__in' => $newArray,
'orderby' => 'post__in'
));
if ($wp_query -> have_posts()):
while ($wp_query -> have_posts()):
the_post();
echo get_the_title() . '<br>';
endwhile;
endif;
?>