2014-01-28 47 views
0

我有一個允許子頁面的事件的自定義頁面類型,我想按照子頁面日期的順序對事件頁面進行排序。頁面排序基於子頁面的自定義字段

所以我的結構是這樣的:

宿營:

  • 德州露營之旅(日期的自定義字段:2014年2月12日)
  • 猶他州露營之旅(的自定義字段日期:2014年3月10日)

募款:

  • 烘烤銷售(日期的自定義字段:2014年1月30日)
  • 無聲拍賣(日期的自定義字段:2014年5月1日)

當查詢我會的網頁像他們一樣按照募捐人的順序返回,然後露營旅行。

回答

0

我假設你正在使用一個類別來分開野營旅行&籌款人,但這種方法應該是關閉其他分離旅行/募捐人的方法。我還假設你想在循環之外或次循環中執行此操作。我沒有測試過這個,但是這樣的事情應該可以做到。

基本上這裏就是打算做:

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 ?> 
相關問題