WordPress.org

Ready to get started?Download WordPress

Forums

Using WP_Query without sorting (6 posts)

  1. SteveStall
    Member
    Posted 3 years ago #

    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?

    Thanks
    PM

  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

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

    http://codex.wordpress.org/Function_Reference/get_post

  3. esmi
    Forum Moderator
    Posted 3 years ago #

    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.

  4. SteveStall
    Member
    Posted 3 years ago #

    @alchymyth

    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();

    Thanks

    //-------------------------------------------------------------------

    @esmi

    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.

    Thanks

  5. esmi
    Forum Moderator
    Posted 3 years ago #

    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.

  6. SteveStall
    Member
    Posted 3 years ago #

    @esmi

    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

Topic Closed

This topic has been closed to new replies.

About this Topic