0

I know this is a convoluted loop (inherited from someone else), and this must be pretty simple: the_permalink and the_title display OK, but why doesn't my post meta eventdatestart echo?.

<?php
$queryObject = new WP_Query( 'post_type=events&posts_per_page=5' );
if ($queryObject->have_posts()) {
?>
<div id="wrapper"">                  
    <h3>Events</h3>
    <ul>
        <?php
    while ($queryObject->have_posts()) {
        $queryObject->the_post();
        ?>
        <li>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

<?php echo get_post_meta(get_the_ID(), 'eventdatestart', true );?>

        </li>
    <?php
    }
    ?>

    </ul>
</div>
    <?php
}
?>
8
  • Maybe because of this part: echo = ? I sometimes do that mistake when I'm in a hurry ;-)
    – birgire
    May 2, 2014 at 16:41
  • Arg, you're right, but that didn't make a difference. There's something not working globally with the post ID, I think. May 2, 2014 at 16:48
  • Sorry to ask, but is eventdatestart the correct meta key? Does it really exists or maybe it's an array?
    – birgire
    May 2, 2014 at 16:54
  • It's the correct key, for one key, not an array. May 2, 2014 at 16:57
  • If you think the post ID does not work in your case, can you simply echo that? Then check for eventdatestart meta key for that specific post id in the database. May 2, 2014 at 17:12

2 Answers 2

1

I think you may need to add global $post after the loop starts based on a similar question found here.

<?php
while ($queryObject->have_posts()) {
   $queryObject->the_post();
   global $post;
?>

I wish I could explain more about why, but I didn't have time to investigate further.

0
0

Can you confirm the ID is correct? try echoing get_the_ID().

If that is the issue, maybe try $queryObject->post->ID. If that works, you could set it as a variable and use within get_the_ID, like this:

$myID = $queryObject->post->ID;
echo get_post_meta(get_the_ID($myID), 'eventdatestart', true );
2
  • Hmmm... get_the_ID() doesn't echo anything. May 2, 2014 at 17:35
  • Did you try echoing $queryObject->post->ID - does that worK?
    – Michelle
    May 2, 2014 at 19:06

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.