1

我正在尝试在一个页面中订购多个帖子类型 我下面的解决方案正在运行,但帖子没有按类型显示

                <?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; ?>
          }
          ?>

它的输出是这样的:

所有类型的帖子:社论

社论

社论

社论

所有类型的帖子:视频

视频

视频

所有类型的帖子:社论//问题

社论

所有类型的帖子:投资组合

文件夹

文件夹

文件夹

文件夹

先感谢您

4

1 回答 1

2

根据Codexpost_type不是orderby参数的允许值之一。

解决问题的一种方法是使用posts_orderbyfilter,如下所示:

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与您的其他论点保持一致。

于 2012-06-30T18:51:08.723 回答