• 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.