tags:

views:

86

answers:

3

Seems like the problem with this is the PHP syntax, but no luck in Wordpress forums. This first code block generates a link to the newest post in category "posts."

<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
<?php endwhile; ?>

This next code block should display the custom field data for the latest post in "posts," with the key of the custom field being "qanda." But it doesn't and it displays nothing.

<?php $my_query = new WP_Query('category_name=posts&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  <?php echo get_post_meta($post->ID, "qanda", $single = true); ?>
<?php endwhile; ?>

Thanks, Mark

+1  A: 

Apart fromthat $single = true should just be true it looks OK... try var_dump instead of echo and see what you get.

Greg
var_dump throws an unexpected T string error, so I guess I don't know what I'm doing with trying that....
songdogtech
Did you put in brackets? It's a function (unlike echo)
Greg
Looks like I found out how to use var_dump, and I got a "false" output. Does that mean I'm not getting the post ID? (Just learning PHP...)
songdogtech
I'm not really familiar enough with wordpress to say
Greg
Ah-hah: the trick is to use $my_query->post->ID instead of $post->ID
songdogtech
Thanks for the help, too....
songdogtech
And I found out the way to dump variables in WordPress:`<?php print var_dump($my_variable); ?>`
songdogtech
A: 

You might need to name it something different. Wordpress might think that you have already done that set of posts, so it is starting at the end, which means it doesn't have anymore posts to process.

Chacha102
Good point, but see my comment to pixeline; Wordpress doesn't seem to care. Kind of surprising it doesn't.
songdogtech
Good point; see pixeline. Thanks.
songdogtech
+1  A: 

try renaming your second query, otherwise Wordpress will think it is already done

<?php 

$my_other_query = new WP_Query('category_name=posts&showposts=1');

while ($my_other_query->have_posts()) : $my_other_query->the_post();
 echo get_post_meta($post->ID, "qanda", true); 
 endwhile; 
?>
pixeline
Actually, Wordpress doesn't care about loops within the main loop. I have other new WP_Queries running on the same page without issue. But I tried your code and it didn't make a difference.
songdogtech
Ah-hah: the trick is to use $my_query->post->ID instead of $post->IDSo, you're right, in a way, because with simply $post->ID, Wordpress does think the query is done, but $my_query->post->ID specifies that particular query loop. Thanks....
songdogtech