0

I want to pull the three most recent posts from a specific category. I have the code to do that, but I'm trying to figure out if there's a way to reverse the order of these three posts when displaying/pulling the data?

Current Code:

 'category_name' => 'dvd-report',
 'posts_per_page' => 3,

 ) );

 if ($cinemasight_header_query->have_posts()) :

 while($cinemasight_header_query->have_posts()) :
 $cinemasight_header_query->the_post();?>

 <span class="Categories_Lower_Right">
 <a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br />
 </span>

 <?php endwhile;

 endif;

 wp_reset_postdata();?>
  • 1
    do you still want the 3 most recent posts, but you just want to reverse their order? – KnightHawk Aug 11 '16 at 18:49
  • Correct. I've updated to include my current loop code. – OscarGuy Aug 12 '16 at 13:40
2

Assuming you are refering to the code of this answer

Taking the query from that answer

$args = array( 
  'category_name'  => 'your-category-slug',
  'posts_per_page' => 3,
);

$wpse_235685_query = new WP_Query( $args );

you would then, to reverse the order of your results, need to modify the query $posts object, like so

$temp_posts = $wpse_235685_query->posts;  // store the posts to work with
$wpse_235685_query->posts = array(); // empty the $posts object
$wpse_235685_query->posts = array_reverse($temp_post); // set back the object to use new reverse order

of course this could be simplified by getting rid of the temporary variable like so

$wpse_235685_query->posts = array_reverse( $wpse_235685_query->posts );

This code should go between your if( $wpse_235685_query->have_posts() ) : and your while( $wpse_235685_query->have_posts() ) : so it doesn't run when the query returns an empty set.

| improve this answer | |
  • Won't that retrieve the three oldest posts? – Andy Macaulay-Brook Aug 11 '16 at 21:10
  • :facepalm @AndyMacaulay-Brook You are right, it will definitely do the opposite of what the op asked by getting the 3 oldest posts! I'll update my answer! – bynicolas Aug 11 '16 at 21:32
  • Fixed the answer to reorder the results and not the entire query logic! – bynicolas Aug 11 '16 at 22:01
  • 1
    Ok. This worked. I used the simplified code (I prefer simplicity). – OscarGuy Aug 12 '16 at 13:47
1

Once you have your array of the three most recent posts you can use array_reverse() (php manual). This way you will still get the three most recent posts like you already do, but now they are in the reverse order.

$array_of_three_recent_posts = array_reverse($array_of_three_recent_posts);
| improve this answer | |
-1

The args:

    $args = array(
        'posts_per_page'      => 3,
        'category_name'       => 'staff',   
        'order'               => 'ASC',
       'orderby'              => 'date'
    );

The query:

    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {

      echo '<ul>';

       while ( $the_query->have_posts() ) {
           $the_query->the_post();
           echo '<li>' . get_the_title() . '</li>';
       }

      echo '</ul>';
      /* Restore original Post Data */
      wp_reset_postdata();

   } else {
    // no posts found
  }
| improve this answer | |
  • 1
    Won't that retrieve the three oldest posts? – Andy Macaulay-Brook Aug 11 '16 at 21:10
  • @ Andy Macaulay-Brook You are correct. I failed to see past the 3 post. Lol, the wrong answer is the right answer! I'll fix my answer. – Frank Thoeny Aug 13 '16 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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