Title: Using the include parameter with get_posts to order the posts
Last modified: August 19, 2016

---

# Using the include parameter with get_posts to order the posts

 *  [fgshepard](https://wordpress.org/support/users/fgshepard/)
 * (@fgshepard)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/)
 * I’m modifying my homepage so that the posts that are displayed are determined
   by a custom field on the homepage. That field includes a comma-separated list
   of post IDs. I then pull the value of the custom field into a variable to create
   an ‘include=’ parameter for get_posts:
 *     ```
       $homePostsIDs = get_post_meta(500, 'Home Posts', true);
       $argument = 'include='.$homePostsIDs';
       $homePosts = get_posts($argument);
       ```
   
 * This works fine. But get_posts always sorts the array of posts. I want them to
   remain in the order specified in my custom field (and used in the ‘include’ parameter).
   Is this possible?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/#post-1222929)
 * I think that the **orderby=none** argument might help you.
 * [http://core.trac.wordpress.org/ticket/9819](http://core.trac.wordpress.org/ticket/9819)
 *  Thread Starter [fgshepard](https://wordpress.org/support/users/fgshepard/)
 * (@fgshepard)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/#post-1223022)
 * Thanks! This is what i’m looking for…
 * But for some reason it’s not working for me. For example, here is the value of
   $argument for the page as it stands right now:
    `'include=456,450&orderby=none'`
   But the page displays 450 first, and then 456. So the orderby parameter doesn’t
   seem to stop the order parameter from operating. It’s sorting them either by 
   ID or Date (not sure which yet) even though I haven’t specified either explicitly.
 * I checked query.php and I have the code mentioned in the article you sent. What’s
   wrong? Any help is much appreciated!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/#post-1223064)
 * Yeah I see it doesn’t work.
 * This seems to work though:
 *     ```
       <?php
         function filter_orderby($orderby = '') {
         $orderby = " FIELD(ID, 350, 194, 306, 1, 421)";
          return $orderby;
         }
       add_filter('posts_orderby', 'filter_orderby');
           $args=array(
             'post__in' => array(350,194,306,1,421),
             'caller_get_posts'=>1
           );
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
             echo '5 ORDERED Posts';
             while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <p> <?php the_category(', '); ?> -
             <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
             endwhile;
           } //if ($my_query)
         wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  Thread Starter [fgshepard](https://wordpress.org/support/users/fgshepard/)
 * (@fgshepard)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/#post-1223171)
 * Thanks again! It will take me awhile to figure out how this one works — I’m new
   to filters. My main concern from looking at it is that the IDs are hard-coded,
   whereas I want to be able to pull them from a custom field. I’ll let you know
   what happens.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using the include parameter with get_posts to order the posts’ is closed
to new replies.

## Tags

 * [field](https://wordpress.org/support/topic-tag/field/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)
 * [order](https://wordpress.org/support/topic-tag/order/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [fgshepard](https://wordpress.org/support/users/fgshepard/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/using-the-include-parameter-with-get_posts/#post-1223171)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
