【问题标题】:wordpress - ordering multiples post typeswordpress - 排序多个帖子类型
【发布时间】:2012-06-30 16:05:26
【问题描述】:

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

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

它的输出是这样的:

所有类型的帖子:社论

社论

社论

社论

所有类型的帖子:视频

视频

视频

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

社论

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

投资组合

投资组合

投资组合

投资组合

提前谢谢你

【问题讨论】:

    标签: wordpress sorting custom-type


    【解决方案1】:

    根据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' =&gt; -1 以与您的其他论点保持一致。

    【讨论】:

    • 没问题;如果有帮助,请随时接受答案和/或投赞成票。这是在这里表达感激的最佳方式;-)
    猜你喜欢
    • 2015-07-10
    • 2018-05-18
    • 2011-08-10
    • 1970-01-01
    • 2015-09-08
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多