WordPress get_post_meta() – Not Return Value

I made some meta_values under a custom post type called ‘Show’. I have confirmed that the meta values are being stored correctly in the DB. So now, I have the following code snippet:

<?php
        $args = array( 'post_type' => 'show', 'posts_per_page' => 1 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
             print get_post_meta($loop->ID, 'date_meta', true);
             the_title();
        endwhile;
?>

The actual loop works wine, as it does display the result of the_title(). But the get_post_meta() isn’t returning anything. The key value is correct, and there is a value for it in the DB.

Read More

Also, if I try to print $loop->ID, it doesn’t return anything either…

thoughts?

Related posts

Leave a Reply

2 comments

  1. needed to add a reference to the global $post variable:

    <?php
        $args = array( 'post_type' => 'show', 'posts_per_page' => 1 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
             global $post;
             print get_post_meta($loop->ID, 'date_meta', true);
             the_title();
        endwhile;
    ?>
    
  2. <?php
    $args = array( 'post_type' => 'show', 'posts_per_page' => 1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
         echo get_post_meta($loop->post_ID, 'date_meta', true);
         the_title();
    endwhile;
    ?>