WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to show a list of articles, but I want to show in the 1st place my second article and after it the last.

Eg: Article 1 Article 2 Article 3 Article 4 to be: Article 2 Article 1 Article 3 Article 4

I'm using this while loop:

$i = 0;
$the_query = new WP_Query( 'showposts=35&offset=0' );
while ($the_query -> have_posts()) : $the_query -> the_post();
if ($i==2) {
echo 'Article2: '.the_title();
if ($i==1) {
echo 'Article1: '.the_title();

But It render: Article1 Article2

I want to render this: Article2 Article1

Thank you in advance!

PS: I want to do this because I want different positions of articles (like CNN) and I don't want to repeat WP_query for all my 34 articles on homepage... Because of this i use if statement.

If you think that this is not a good question please do not -1 me, I will delete my post if you want.

share|improve this question
up vote 2 down vote accepted

Prepare the query before you loop over it:

$first  = array_shift( $the_query->posts );
$second = array_shift( $the_query->posts );

// now re-add in reverse order
array_unshift( $the_query->posts, $first );
array_unshift( $the_query->posts, $second ); // this is now the first item

while ($the_query -> have_posts()) : $the_query -> the_post(); // regular loop
share|improve this answer
Thank you for your response. Unfortunately is not useful for me. I'm trying to make something like nyt.com . On my first column i want to show articles with offset: 1 3 4 5 6 7 and in my second collumn i want to show 2 8 9 10. – NETCreator Hosting - WebDesign Jul 9 '13 at 8:09
I don't want to repeat WP_Query (I have 34 articles on homepage) – NETCreator Hosting - WebDesign Jul 9 '13 at 8:10
This is exactly what you are asking for: it switches the first two items in your custom query. – toscho Jul 9 '13 at 8:13
yes, thank you... I will ask a new question with this. – NETCreator Hosting - WebDesign Jul 9 '13 at 8:14

You can access the array of posts with $the_query->posts :

    $i = 0;
    $the_query = new WP_Query( 'showposts=35&offset=0' );
    // swap post 1 with post 2
    $temp_post = $the_query->posts[0];
    $the_query->posts[0] = $the_query->posts[1];
    $the_query->posts[1] = $temp_post;
    while ($the_query->have_posts()) : $the_query ->the_post(); ?>
       // do your regular loop
<?php endwhile; ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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