WordPress按帖子类型订购帖子 [英] Wordpress order posts by post type

查看:93
本文介绍了WordPress按帖子类型订购帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个从2种帖子类型中获取帖子的系统,并且我想按帖子类型显示帖子顺序.我正在尝试先显示一种帖子类型的帖子,然后再显示另一种帖子类型的帖子,但是我使用的查询混合了所有内容.谁能为此提出一个好的解决方案.这是我的代码.

I am trying to create a system which fetches post from 2 post types and i want to display the posts order by their post type. I am trying to first show the posts from one post type and then another post type but the query i used mixes it all. Can any one suggest a good solution for this. Here is my code.

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
 $args = array(

     'post_type' => array('review','directory'),
     'orderby' => 'name',
     'order' => 'ASC',
     'posts_per_page'  => '4',
     'paged'          => $paged ,
     'tax_query' => array( array(
        'taxonomy' => 'Reviews',
        'field' => 'id',
        'terms' => $cat_id
    ), ),
     );

 query_posts($args);
        if (have_posts()) :
       while (have_posts()) : the_post();
      $product_terms = wp_get_post_terms(get_the_ID(), 'Reviews', array("fields" => "all", "order" => "DESC"));
      $postype=get_post_type( get_the_ID() );
      if($postype=='review')
      {
     ?>   

        <div class="review-post">
          <a href="http://<?php the_permalink(); ?>" target="_blank"><h3><?php the_title(); ?></h3></a>
          <div class="review-cat-new">
            <?php echo get_the_term_list( $post->ID, 'Reviews', 'Category: ', ', ', '' ); ?>
          </div>

          <?php
          if(get_field('see_it'))
              $seeit_text= get_field('see_it');
         if(get_field('skip_it'))
              $skipit_text= get_field('skip_it');
         ?>
          <div class="see-skip">
            <p class="see-it"><span>See it:</span>
                <?php echo $seeit_text; ?>
            </p>
            <p class="skip-it"><span>Skip it:</span>
                <?php echo $skipit_text;  ?>
            </p>
          </div>
          <?php echo custom_field_excerpt(); ?>
        </div>
          <?php }
          else
          {
              ?>
                <div class="review-post">
          <a href="h<?php the_permalink(); ?>" target="_blank"><h3><?php the_title(); ?></h3></a>
          <div class="review-cat-new">
            <?php echo get_the_term_list( get_the_ID(), 'Reviews', 'Category: ', ', ', '' ); ?>
          </div>


          <?php echo the_field('enter_content_direc'); ?>
          <?php
          if(get_field('enter_textdirec'))
              $text= get_field('enter_textdirec');
         if(get_field('enter_linkdirec'))
              $textlink= get_field('enter_linkdirec');
         ?>
          <div class="see-skip">
            <p class="see-it direc"><span><a  target="_blank" style="color:#5D6D71; text-transform: lowercase;" href="<?php echo $textlink;?>"><?php echo $textlink;?>  </a></span>

            </p>
          </div>
        </div>
          <?php }
          endwhile;
                  echo '<div class="paging">';
                 wp_pagenavi();
                 echo '</div>';
                endif;

有没有一种方法可以让我首先显示评论形式的帖子,然后显示目录?

So is there a way so that i can first show the posts form reviews and then directory?

推荐答案

最后,我得到了解决方案,可以使用过滤器完成.

Finally i got the solution and it can be done using a filter.

解决方案如下.

add_filter( 'posts_request' , 'modify_request' );
function modify_request( $query) {
    global $wpdb;
    if(strstr($query,"post_type IN ('review', 'directory')")){
        $where = str_replace("ORDER BY {$wpdb->posts}.post_date","ORDER BY {$wpdb->posts}.post_type",$query);
    }
    return $where;
}

这篇关于WordPress按帖子类型订购帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆