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

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?


 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
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(); ?>


 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


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.