0

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

3
  • 1
    Aaaah, all the opening and closing tags are burning my eyes. Commented Jul 23, 2009 at 19:10
  • 1
    <?php $var = "Mine"; ?><?php print $var . " "; ?><?php print "too"; ?> Commented Jul 23, 2009 at 19:12
  • Yup, I know: it's Wordpress. They make PHP easy to parse for those of us who are learning! Cleanse Thine Eyes with pure PHP.... Commented Jul 23, 2009 at 20:32

3 Answers 3

1

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; 
?>
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Ah-hah: the trick is to use $my_query->post->ID instead of $post->ID So, 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....
1

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

6 Comments

var_dump throws an unexpected T string error, so I guess I don't know what I'm doing with trying that....
Did you put in brackets? It's a function (unlike echo)
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...)
I'm not really familiar enough with wordpress to say
Ah-hah: the trick is to use $my_query->post->ID instead of $post->ID
|
0

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.

1 Comment

Good point, but see my comment to pixeline; Wordpress doesn't seem to care. Kind of surprising it doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.