6

I'm am displaying the last 5 posts of a custom post type 'show'.

This gives me the latest post first.

<?php
$args = array(
    'post_type' => 'show',
    'posts_per_page' => 5,
    'order' => 'DESC'
    );
$home_shows = new WP_Query($args);
var_dump($home_shows);
?>

What I need is to actually have the earliest (of the array of the latest shows) first, and the latest show (in that array of latest shows) last.

I am now currently getting (the show date via a custom field meta value):

3/11/12, 3/7/12, 3/4/12, 3/2/12, 2/30/12 etc.

I need: 2/30/12, 3/1/12, 3/4/12, 3/7/12, 3/11/12,

I tried using php's array_reverse like so (added to the above code):

$reversed_shows = array_reverse( $home_shows->posts );

Which gave me really odd results (displayed completely different parts of the post, array order was off).

Any ideas?

4 Answers 4

8

I figured out what I was doing wrong. A simple beginners mistake.

Array_reverse was working properly, but I wasn't then reassigning the reversed array back to the $home_shows WP_Query, hence not seeing any change.

Here is the answer, and my revised code.

<?php
                    $args = array(
                        'post_type' => 'show',
                        'posts_per_page' => 5,
                        'order' => 'DESC',
                    );
                    $home_shows = new WP_Query($args);
                    //reverse the order of the posts, latest last
                    $array_rev = array_reverse($home_shows->posts);
                    //reassign the reversed posts array to the $home_shows object
                    $home_shows->posts = $array_rev;
                ?>
                <?php $captions = array(); ?>
                <?php if ( $home_shows->have_posts() ) : ?>         
                    <?php while ( $home_shows->have_posts() ) : $home_shows->the_post(); ?>

Thanks for the replies, glad I figured this one out.

1
  • Don't forget to mark your answer as accepted!
    – Burgi
    Commented Nov 21, 2016 at 11:39
1

Remove all the custom field mess and add an 'order' => 'ASC' to the args array! And you're done!

3
  • What custom field mess? The custom post type? That's needed, as the site has a few custom post types to organize things. I'll try to explain what I mean better via a post edit
    – Squadrons
    Commented Apr 3, 2012 at 4:18
  • I apologise for the wrong choice of words. Custom fields are available for default post types too. Just because its a custom post type, all you need is a 'post_type', which you already have. Is it true that you want to display the last posts first? Commented Apr 3, 2012 at 4:27
  • 1
    this doesn't actually solve the problem because if you're only querying the 10 most recent posts, and you run order ASC it will give you the oldest 10 posts. Commented Jul 26, 2016 at 23:42
0
    <?php
    $args = array(
        'post_type' => 'show',
        'posts_per_page' => 5,
        'order' => 'desc'
        );
    $home_shows = new WP_Query($args);
 //   var_dump($home_shows);

echo "<pre>";
print_r($home_shows->posts);
echo "</pre>";



$array_rev = array_reverse($home_shows->posts);
echo "<pre>";
print_r($array_rev);
echo "</pre>";
  ?>

I tried above for my custom post type...

results post ids : 240,239,238,237 for reverse : 237,238,239,240

There must be something else in proble at your end......

5
  • Orderby is set to date by default! Commented Apr 3, 2012 at 6:33
  • @RutwickGangurde have edited my answer... Commented Apr 3, 2012 at 6:52
  • Why the array reverse? Just switch the order to ASC and you get the last posts first. Commented Apr 3, 2012 at 6:58
  • @RutwickGangurde I" think you dont understand his requirement... what he wants is to get last 5 posts and then display them in order that out of that last five smallest date post is first..... Commented Apr 3, 2012 at 7:03
  • All right, didn't notice that earlier. I just read his edited question! Commented Apr 3, 2012 at 7:15
0

If you need to reorder the main query I suggest to use the following hook:

add_action('the_posts',function($posts,$query){
    
    if( $query->is_main_query() ){
        
        usort($posts,function ($a, $b) {
            
            return strtotime($b->post_modified) - strtotime($a->post_modified);
        });
    }
    
    return $posts;
    
},10,2);

In this example I am reordering the posts by post_modified date but you can use another logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.