get_meta_data within a loop

I’m trying to make a simple widget that displays a list of posts, and is followed by some metadata from each post.

Here is my code:
$eventdate contains the metadata i need to retrieve.

Read More
query_posts('');

if (have_posts()) : 
    echo "<ul>";
    while (have_posts()) : the_post(); 
        $eventdate = get_post_meta($post->ID, 'event-date', true);
        echo "<li>".get_the_title()." - ".$eventdate."</li>";
    endwhile;
    echo "</ul>";
endif; 
wp_reset_query();

I read an old post on the wordpress support forums about custom loops like this, and I’m fairly certain that I’m not calling the $post_id properly, because if I manually insert a post_id it calls the correct data.. but either way I’m unsure where to go from here. Any help would be appreciated.

Related posts

Leave a Reply

2 comments

  1. query_posts() is for main query, it would be more wise to use get_posts() function to get you events.

    // Modify get_posts args for your needs
    $events = get_posts( array ( 'post_type' => 'events' ) );
    
    if ( $events ) {
        echo '<ul>';
    
        foreach ( $events as $event ) {
            $event_date = get_post_meta( $event->ID, 'event-date', true );
    
            echo '<li>' . get_the_title( $event->ID ) . ' - ' . $event_date . '</li>';
        }
    
        echo '</ul>';
    }