Order by & include array by specific post ids

I have the following:

<?php $num_cols = 2; // set the number of columns here


            $args = array(
                    'post_type' => 'testimonials',
                    'posts_per_page' => 4,
                    'orderby' => 'ID', 
                    'include' => '883, 563, 568, 106',                                  
                    'order' => ''           

                    ); 
            query_posts($args);
            if (have_posts()) :
            for ( $i=1 ; $i <= $num_cols; $i++ ) :
            echo '<div id="col-'.$i.'" class="col">';
            $counter = $num_cols + 1 - $i;
            while (have_posts()) : the_post();

            if( $counter%$num_cols == 0 ) : ?>
                <div id="box">

                <?php the_post_thumbnail('post-thumbnail', array('title' => '', 'alt' => '')); ?>



                <?php the_excerpt();?>
                </div>
                <?php endif; $counter++;
                  endwhile;
                  echo '</div>'; 
                  endfor;
                  endif; 
                wp_reset_query(); 
                ?>

I thought that simply putting the post Ids, I could have the specific posts appear, and only those posts. Unfortunately, that doesn’t seem to be the case. So I was wondering if anyone knew how I can accomplish this.

Read More

Thanks in advance!

Related posts

Leave a Reply

3 comments

  1. use post__in key instead of include to get posts from the specific post ids.

    $args = array(
        'post_type' => 'testimonials',
        'posts_per_page' => 4,
        'orderby' => 'ID', 
        'post__in' => array(883, 563, 568, 106),
    ); 
    

    And to order posts by the given posts ids, you can use the following array.

    $args = array(
           'post_type' => 'testimonials',
           'posts_per_page' => 4,
          'orderby' => 'post__in', 
          'post__in' => array(883, 563, 568, 106),
       );
    
  2. $args = array(
        'post_type' => 'testimonials',
        'posts_per_page' => 4,
        'orderby' => 'post__in', 
        'post__in' => array(883, 563, 568, 106)
    ); 
    

    Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in

  3. for me it works removing the colon “; ” and replacing for a coma “,” like this:

                       'orderby' => 'post__in',
                       'post__in' => array(185, 157, 163),