Apologies if the title isn't clear, can't quite think how to word this.

I'm getting a pretty standard list of posts like this:

            SELECT * FROM $wpdb->posts 
            WHERE post_type='post' AND post_status='publish' 
            ORDER BY post_date DESC LIMIT 6
        ", OBJECT);
    $firstpost = array_shift($pageposts);  

But this isn't returning what I expect. Instead of $firstpost conatining the first post in the array $pageposts (like the docs say it should), it contains the oldest - and $pageposts still also contains that post (which it shouldn't according to the docs), however the latest post is missing. I don't know what the Gordon Bennet is going on with this, I've also tried array_pop() which should do the opposite of array_shift(), but with similarly strange results (latest post shown but $firstpost still contains the wrong value).

OK I've waffled on a bit, so to conclude, here's what I'm trying to do: I want to make a single query, get the result, and print out the latest post in one area of the page and then the rest of the posts in another area. Trouble is, in the HTML, the older posts come before the one new post, so I need to split it into a separate variable and make sure the older posts don't show this new one with the others.

Does this make sense?

Any help appreciated :)

  • just occurred to me that this is probably better asked on SO as it's more to do with PHP, so I'll post it there instead. thanks anyway :) – jammypeach Mar 26 '12 at 11:32
  • use get_posts please – soulseekah Mar 26 '12 at 11:44
  • @Soulseekah why? is it more efficient, or should I be using it because I'm not doing anything it doesn't do already? – jammypeach Mar 26 '12 at 11:48
  • 1
    You're avoiding caching, for one. And as you grow it, you are bound to miss some input sanitization. get_posts is more readable and easier to use. If you're not working with a 10000000000 page requests without caching you'll barely notice any difference between raw SQL. Building on the efficiency logic, why don't you mysql_query instead? That's even more efficient, no? – soulseekah Mar 26 '12 at 11:50
  • Fair points, I'll switch to get_posts, thanks. Also I've figured out my problem atleast as far as array_shift goes - it does exactly what it should do. I have an error where I'm outputting the data, time to go and debug :) – jammypeach Mar 26 '12 at 11:52

once you have set of result in particular order you can achieve it simply as below : -

            SELECT * FROM $wpdb->posts 
            WHERE post_type='post' AND post_status='publish' 
            ORDER BY post_date DESC LIMIT 6
        ", OBJECT);

First time

foreach($pageposts as $pageposts_first){
//here goes your first post
if($i==1) break;

For rest of posts

foreach($pageposts as $pageposts_rest){
   if($i==1) continue;
  //here goes you rest of posts

You can do same thing even if you use get_posts instead..

  • yep, that helped me, thanks :) feel a bit dense now lol – jammypeach Mar 26 '12 at 13:11
  • @jammypeach Sometime its good to think simple!!! – Rajeev Vyas Mar 26 '12 at 13:15

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.