Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

    $pageposts=$wpdb->get_results("
            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 :)

share|improve this question
    
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
add comment

1 Answer 1

up vote 2 down vote accepted

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

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

First time

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

For rest of posts

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

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

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.