我有一个允许子页面的自定义页面类型的事件,我想按子页面的日期顺序对事件页面进行排序。
所以我的结构是这样的:
露营旅行:
- 德克萨斯露营之旅(自定义日期字段:2/12/2014)
- 犹他州露营之旅(自定义日期字段:2014 年 3 月 10 日)
筹款人:
- 烘焙销售(日期的自定义字段:2014 年 1 月 30 日)
- 无声拍卖(自定义日期字段:2014 年 5 月 1 日)
ETC
在查询页面时,我希望它们按筹款人然后露营旅行的顺序返回。
我有一个允许子页面的自定义页面类型的事件,我想按子页面的日期顺序对事件页面进行排序。
所以我的结构是这样的:
露营旅行:
筹款人:
ETC
在查询页面时,我希望它们按筹款人然后露营旅行的顺序返回。
我假设您正在使用一个类别来区分露营旅行和筹款活动,但是对于其他区分旅行/筹款活动的方法,这种方法应该很接近。我还假设您想在循环之外执行此操作,或者作为辅助循环。我还没有测试过这个,但是像这样的东西应该可以解决问题。
基本上这是要做的事情:
1) 查询筹款人和露营旅行,按日期排序。
2) 将两个查询合并在一起,使筹款人排在第一位,露营旅行排在第二位。
3)循环浏览每个返回的帖子,并根据您的喜好标记信息。
代码:
<?php //Enter your information for each variable:
$post_type = 'enter_your_custom_post_type_slug';
$fundraiserCatID = 'enter_your_fundaiser_category_id';
$campingCatID = 'enter_your_camping_category_id';
$acfDateFieldName = 'enter_your_date_acf_field_slug';
//Setup each Query args
$fundraiserAgrs = array('post_type' => $post_type, 'cat' => $fundraiserCatID, 'orderby' => 'meta_value', 'meta_key' => $acfDateFieldName, 'order' => 'ASC');
$campingAgrs = array('post_type' => $post_type, 'cat' => $fundraiserCatID, 'orderby' => 'meta_value', 'meta_key' => $acfDateFieldName, 'order' => 'ASC');
$fundraisers = get_posts($fundraiserArgs);
$campingTrips = get_posts($campingArgs);
$events = array_push($fundraisers, $campingTrips); //merge the two queries, with fundraisers first, then camping trips
if($events) : foreach($events as $event): //If we have $events, loop through each event
//Do what you will with the $event content ?>
<h1><?php echo $event->post_title; ?></h1>
<?php echo $event->post_content; ?>
<h6>Date: <?php the_field($acfDateFieldName, $event->ID); ?></h6>
<?php endforeach; endif; //End the loop and the conditional ?>