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.

Alright y'all. I want to dynamically display the information of the 2 most current posts to the screen. So far I have only been able to get results by hard coding the ID into the query. Is there a way to retrieve this data dynamically?

query_posts('showposts=2'); 

 while (have_posts()) : the_post(); ?>

<?php $x = get_the_ID(); 
echo $x;
$postStuff = $wpdb->get_row('select * from wp_posts where ID = "$x", ARRAY_A);
print_r($postStuff); ?>

<?php endwhile;?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

The correct way:

$my_query = new WP_Query('posts_per_page=2'); 

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

<?php

 global $post;
 print_r($post);  // <-- this is your postStuff

?>

<?php endwhile;?>

<?php wp_reset_query(); ?>

Normally, you dont't need to globalize the post. There are helper functions that should be used instead to fetch information from the current post

share|improve this answer
    
yep, the helper functions are called template tags. The post related ones are here: codex.wordpress.org/Template_Tags#Post_tags –  offroff Apr 21 '12 at 16:16
    
One Trick... you did it! Well done. –  bkbarton Apr 21 '12 at 19:23

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.