Order posts in a $wp_query manually

Build a custom WordPress query and order the posts however you want them. Notice that this won't work properly if you page results, so it is only useful in very specific cases.

Btw, wrap everything into your class and get rid o the global var when you use this ;-)

<?php

global $posts;
$posts = array( 1, 2, 3, 4, 5 );
$args = array(
    'post__in'             => $posts,
    'posts_per_page'   => 5, 
    'caller_get_posts'  => 1 // ignore sticky status
);
$my_query = new WP_Query( $args );
/**
 * Order an array of objects by object property
 */
function orderby( $a, $b ) {
    global $posts;
    $apos   = array_search( $a->ID, $posts );
    $bpos   = array_search( $b->ID, $posts );
    return ( $apos < $bpos ) ? -1 : 1;
}
usort( $my_query->posts, "orderby" );
?>

Published on Nov. 9, 2010 at 3:27 a.m. by Nicolas and tagged order posts, PHP, WordPress, WordPress plugin, WordPress post, WordPress query, WordPress theme. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!

7 comments

  1. avatar
    wrote this comment on
    Thanks! This is exactly what I needed.

    <?php
    global $posts;
    $posts = array(6, 9, 19, 21, 34, 23, 26, 28);
    $home_query = new WP_Query( array(
    'post__in' => $posts,
    'posts_per_page' => count($posts),
    'post_type' => 'page'
    ));
    function home_orderby( $a, $b ) {
    global $posts;
    $apos = array_search( $a->ID, $posts );
    $bpos = array_search( $b->ID, $posts );
    return ( $apos < $bpos ) ? -1 : 1;
    }
    usort( $home_query->posts, "home_orderby" );
    while ($home_query->have_posts()) : $home_query->the_post();
    ?>
    <div <?php post_class() ?> id="page-<?php the_ID(); ?>">
    <h1><?php the_title();?></h1>
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>

    Reply to this comment
  2. avatar
    wrote this comment on
    Thanks for your awesome plugins, Nicolas! I came here following a link from the readme.txt of Better Related 0.4.3.3. I wasn't able to get the output sorted by score for customly placed code. Could you tell how to get it working?

    <?php $scores = the_related_get_scores(); // pass the post ID if outside of the loop
    $posts = array_slice( array_keys( $scores ), 0, 10 ); // keep only the the amount of results in the last number
    $args = array(
    'post__in' => $posts,
    'posts_per_page' => 10,
    'caller_get_posts' => 1 // ignore sticky status
    );
    $my_query = new WP_Query( $args );
    if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
    $my_query->the_post();
    echo '<p class="clearfix"><a href="' . get_permalink( get_the_ID() ) . '">';
    if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'thumbnail', array('class' => 'alignleft') );
    the_title();
    }
    echo '</a></p>';
    }
    }?>

    Thanks in advance!
    Reply to this comment
  3. avatar
    wrote this comment on
    Hi! Thanks for reminding me... I've though a bit about the problem and should be able to add a solution to the plugin once I have a few free hours.
    Reply to this comment
  4. avatar
    wrote this comment on
    Thanks for the insight Nicolas, I used it on one of my websites and wrote a short article on it: http://www.falise.com/blog/wordpress-code/sorting-loop/

    If you use a closure, you don't need to use a global variable nor a class!
    http://php.net/manual/en/function.usort.php#function.usort.examples.closure
    Reply to this comment
    1. avatar
      wrote this comment on
      Thanks for the input! Yeah, my code could certainly use some refactoring :-)
      Reply to this comment
  5. avatar
    wrote this comment on
    How do i call a custom post type thumbnail

    Here is the code

    `<?php
    the_related(
    get_the_ID(),
    array(
    'usept' => array(
    'gb_deal' => true
    ),
    'maxresults' => 3,
    'storage_id' => 'more-better-related-'
    )
    );

    if ( has_post_thumbnail('gbs_deal') ) {
    the_post_thumbnail( 'gls_deal' );
    }


    ?>

    Reply to this comment
    1. avatar
      wrote this comment on
      Looks like you're missing a loop. http://codex.wordpress.org/The_Loop
      Reply to this comment

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.