Support » Fixing WordPress » Using WP_Query without sorting

  • Hello,

    I’m using WP_Query to retrieve several posts. I know witch posts I want and in witch order so I created an array with all the required IDs. Unfortunately the result of WP_Query does not reflect the order of my array with the IDs. Tried to use the orderby=none but no result:

    $ids = array (60, 23, 78, 46, 105, 130)
    $args = array (
    	'posts_per_page' => count($ids),
    	'post__in' => $ids,
    	'orderby' => 'none'
    $query = new WP_Query ( $args );
    if ( $query->have_posts() ) : while...[bla bla bla]

    The result is always in ID descending order.

    Can anyone help me?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael


    Forum Moderator

    you could try and use a ‘foreach’ loop with you $ids array, and use get_post() or so.

    If you set orderby to none, doesn’t that mean you’ll get the posts in the order they’re retrieved from the database – which will be in id order descending, IIRC? So you may be getting exactly what you asked for.

    I’m not sure that you can specific the order of the post via the post__in array.


    Hi alchymyth, thanks for the reply.

    that could work, though forces the server to compute more then necessary.
    How could I use your solution and still use the wordpress typical loop:

    if ($query->have_posts()) :while ($query->have_posts()) : $query->the_post();




    Hi esmi, thanks for the reply.

    One way or the other I always get a sort. If I don’t define orderby=none wordpress code will use ID has default.


    Why not add custom fields to your selected posts? If you were a little imaginative with the meta-value, you could then use meta_value_num to order your posts.


    Actually posts are edited with magic-fields plugin.

    I have a single post with several relationship combo-box that order 10 posts. A combo for each post. Combos point to all website posts id.

    In PHP I get all posts ids listed correctly but when I make the final call to retrieve all identified posts orders come incorrect

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using WP_Query without sorting’ is closed to new replies.