Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress order posts by post type

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?

like image 463
Nirmal Ram Avatar asked Dec 20 '22 07:12

Nirmal Ram


2 Answers

Since Wordpress 4.0 you can just pass the option type (post_type) to the orderby parameter in the WP_Query object.

orderby (string | array) - Sort retrieved posts by parameter. To order by post type just pass:

  • 'type' - Order by post type (available since Version 4.0). ('post_type' is also accepted.)
like image 135
Musi Avatar answered Jan 08 '23 01:01

Musi


As seen in the docs https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters , you can use the regular wp query by passing an array to 'orderby' :

$wp_query = new WP_Query(
  array(
      's' => $search,
      'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
      'paged'=>$paged
  )
);
if ( $wp_query->have_posts() ) {
    while ( $wp_query->have_posts() ) {
        $wp_query->the_post();
        // do your loop stufff here
    }
}
like image 29
rafa226 Avatar answered Jan 08 '23 03:01

rafa226