2012-06-30 91 views
1

我想要在一個頁面 我下面的解決方案正在訂購倍數文章類型,但職位不按類型類型完全WordPress的 - 訂貨倍數文章類型

   <?php $args = array_merge($wp_query->query, 
       array('post_type' =>array('editorial','video','portfolio'),'posts_per_page=-1','orderby'=>'post_type')); 
       } 
       query_posts($args); 

       $postType=''; 

       if (have_posts()) : while (have_posts()) : the_post(); 
       $PT = get_post_type($post->ID); 
       if($postType != $PT){ 
       $postType = $PT; 
       echo '<p class="clearfix"><h1>All posts of type : '.$postType.'</h1></p>'; 
       } 
       ?> 

     <?php 
     if($postType == 'editorial'){ ?> 
      <?php echo $postType; ?> 
      <?php }elseif($postType == 'video'){ ?> 
      <?php echo $postType; ?> 
      <?php }elseif($postType == 'portfolio'){ 
      <?php echo $postType; ?> 
      } 
      ?> 

後顯示,它像輸出:

類型的所有帖子:編輯

社論

社論

社論

所有類型的職位:視頻

視頻

視頻

類型的所有帖子:編輯//問題

社論

類型的所有帖子:portfolio

組合

組合

組合

組合

預先感謝您

回答

2

the Codexpost_type不是爲orderby參數的允許值之一。在你的問題

的一種方法是使用posts_orderby filter,像這樣:

function order_posts_by_type($original_orderby_statement) { 
    global $wpdb; 
    return $wpdb->posts .".post_type ASC"; 
} 

add_filter('posts_orderby', 'order_posts_by_type'); 

$custom_query = new WP_Query(array(
    'post_type' =>array('editorial','video','portfolio'), 
    'posts_per_page' => '-1', 
    'orderby'=>'post_type', 
    'order' => 'ASC' 
)); 

remove_filter('posts_orderby', 'order_posts_by_type'); 

FWIW,我建議改變'posts_per_page=-1''posts_per_page' => -1要與你的其他參數保持一致。

+0

非常感謝您的解決方案和您的建議 – lococo

+0

沒問題;如果它有幫助,可以隨時接受答案和/或給它一個upvote。這是在這裏展示欣賞的最佳方式;-) – Hobo

+0

不錯 - 謝謝! – Hobo