页面排序基于子页面的自定义字段
问题描述:
我有一个允许子页面的事件的自定义页面类型,我想按照子页面日期的顺序对事件页面进行排序。页面排序基于子页面的自定义字段
所以我的结构是这样的:
宿营:
- 德州露营之旅(日期的自定义字段:2014年2月12日)
- 犹他州露营之旅(的自定义字段日期:2014年3月10日)
募款:
- 烘烤销售(日期的自定义字段:2014年1月30日)
- 无声拍卖(日期的自定义字段:2014年5月1日)
等
当查询我会的网页像他们一样按照募捐人的顺序返回,然后露营旅行。
答
我假设你正在使用一个类别来分开野营旅行&筹款人,但这种方法应该是关闭其他分离旅行/募捐人的方法。我还假设你想在循环之外或次循环中执行此操作。我没有测试过这个,但是这样的事情应该可以做到。
基本上这里就是打算做:
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 ?>