Title: get_posts &#8211; undesirable behavior
Last modified: May 23, 2017

---

# get_posts – undesirable behavior

 *  [conconovaloff](https://wordpress.org/support/users/conconovaloff/)
 * (@conconovaloff)
 * [9 years ago](https://wordpress.org/support/topic/get_posts-undesirable-behavior/)
 * Imagine: you trying get last(DESC) product by ID or menu_order:
 *     ```
                   $post_list = get_posts([
                       'post_type'   => 'product',
                       'numberposts' => 1,
                       'post_status' => 'publish',
                       'orderby'     => 'ID',
                       'order'       => 'DESC',
                   ]);
       ```
   
 * But instead we get: order by ASC,
 * How did this happen?:
    == 1 == file: wp-includes/class-wp-query.php function:
   get_posts() code: do_action_ref_array( ‘pre_get_posts’, array( &$this ) );
 * == 2 ==
    file: wp-content/plugins/simple-custom-post-order/simple-custom-post-
   order.php function: scporder_pre_get_posts($wp_query) code:
 *     ```
       ...
                   if (isset($wp_query->query['post_type'])) {
                       if (!is_array($wp_query->query['post_type'])) {
                           if (in_array($wp_query->query['post_type'], $objects)) {
                               $active = true;
                           }
                       }
       ...
                   if (isset($wp_query->query['suppress_filters'])) {
                       if ($wp_query->get('orderby') == 'date')  # <--- not change orderby "ID"
                           $wp_query->set('orderby', 'menu_order');
                       if ($wp_query->get('order') == 'DESC')  # <--- but change order to ASC
                           $wp_query->set('order', 'ASC');
       ```
   
 * How resolve this problem?
    Pffff…. no problem. =)
 *     ```
                   $post_list = get_posts([
                       'post_type'   => 'product',
                       'numberposts' => 1,
                       'post_status' => 'publish',
                       'orderby'     => 'ID',
                       'order'       => 'desc', # <--- to lowercase this =)
                   ]);
       ```
   

The topic ‘get_posts – undesirable behavior’ is closed to new replies.

 * ![](https://ps.w.org/simple-custom-post-order/assets/icon-256x256.jpg?rev=2969435)
 * [Simple Custom Post Order](https://wordpress.org/plugins/simple-custom-post-order/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/simple-custom-post-order/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/simple-custom-post-order/)
 * [Active Topics](https://wordpress.org/support/plugin/simple-custom-post-order/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/simple-custom-post-order/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/simple-custom-post-order/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [conconovaloff](https://wordpress.org/support/users/conconovaloff/)
 * Last activity: [9 years ago](https://wordpress.org/support/topic/get_posts-undesirable-behavior/)
 * Status: not resolved