0

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;?>
1

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

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.