• Resolved Scott Paterson

    (@scottpaterson)


    Hi,

    In my plugin I am getting posts using the following:

    $args = array(
    'post_type' => 'wpplugin_pp_order',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'orderby' => 'ID'
    );
    
    $posts = get_posts($args);

    The problem is the result (post_id) is returning like so:
    3000
    3001
    3002
    3003
    2999
    2998

    How can I put in the correct order?

    Thanks,
    Scott

Viewing 6 replies - 1 through 6 (of 6 total)
  • Your post here is identical to this one on stack exchange that is 4 years old:

    https://stackoverflow.com/questions/30769650/get-posts-orderby-desc-wrong-order

    Why?

    That said, have a look here:

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    What is the plugin? Where did you get it?

    Thread Starter Scott Paterson

    (@scottpaterson)

    Hi Pioneer Web Design,

    Thanks for your help. To answer your questions:
    1. I posted that at stackoverflow – it was 4 hours ago, not 4 years ago.
    2. I did already look at that page. I mean according to that I am doing it correctly.
    3. I made the plugin. It is a new one that I am working on.

    Thanks,
    Scott

    Years, Hours..must be late…

    What you likely need to do is a NEW query.

    If you look at all the examples given, they use a new query.

    $query = new WP_Query( $args );
    Thread Starter Scott Paterson

    (@scottpaterson)

    Thanks, ill give that a try and see what happens.

    Thread Starter Scott Paterson

    (@scottpaterson)

    Hi Pioneer Web Design,

    Thanks for your help. I ended up finding the problem, and a solution.

    I dumped the array from above and it was in the correct order. The problem was this chunk of code from some example I found online for extending wp_list_table, seems like everyone is using it.

    function usort_reorder($a,$b) {
    $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'order';
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
    $result = strcmp($a[$orderby], $b[$orderby]);
    return ($order==='asc') ? $result : -$result;
    }
    usort($data, 'usort_reorder');
    }

    The above is need and works as it it suppose to if the user wants to manually sort the columns, but not by default. So I simply wrapped an if statement around it:

    if (isset($_REQUEST['orderby'])) {
    function usort_reorder($a,$b) {
    $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'order';
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
    $result = strcmp($a[$orderby], $b[$orderby]);
    return ($order==='asc') ? $result : -$result;
    }
    usort($data, 'usort_reorder');
    }

    And this solved the problem. Thanks for your help! I really appreciate it.

    Thanks,
    Scott

    That seems to be a much more complex method than just doing as noted in the link given, a new query, but glad you found a solution that you appear to be comfortable with.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_posts – order desc’ is closed to new replies.