Created
September 7, 2012 08:27
-
-
Save nmec/3664336 to your computer and use it in GitHub Desktop.
Sorting a WordPress loop by post__in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$posts = array(5, 2, 43, 12); | |
// Get the posts | |
$my_loop = new WP_Query(array( | |
'post__in' => $posts, | |
'post_type' => 'any', | |
'posts_per_page' => -1, | |
)); | |
// Re order the posts | |
$reorder_loop = array(); | |
foreach($posts as $rpid) | |
foreach($my_loop->posts as $index => $fpid) | |
if($fpid->ID === $rpid) $reorder_loop[] = $my_loop->posts[$index]; | |
$my_loop->posts = $reorder_loop; | |
// Start the loop | |
if ($my_loop->have_posts()) : while ($my_loop->have_posts()) : $my_loop->the_post(); | |
the_title(); | |
the_content(); | |
endwhile; endif; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$posts = array(5, 2, 43, 12); | |
// Get the posts | |
$my_loop = new WP_Query(array( | |
'post__in' => $posts, | |
'post_type' => 'any', | |
'posts_per_page' => -1, | |
'orderby' => 'post__in' | |
)); | |
// Start the loop | |
if ($my_loop->have_posts()) : while ($my_loop->have_posts()) : $my_loop->the_post(); | |
the_title(); | |
the_content(); | |
endwhile; endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This way sticky posts get appended automatically. After using
$my_loop = new WP_Query(array( 'post__in' => $posts, 'post_type' => 'any', 'posts_per_page' => -1, 'orderby' => 'post__in', 'ignore_sticky_posts' => 1 ));
Then 'orderby' => 'post__in' doesn't work. Any solution for this?