I've a CPT incorporating CMB2. I loop through every category and show each post within that category. My issue is that the first post in each category doesn't display certain info from two metaboxes: Label Name ($label) and Release Date:($date), but the other posts in each category display this info. So far I have about 8 categories and this happens in all of them.

When I var_dump($label);, the first post of each category displays string(0) "", even though there is something there in the admin side. All other posts are fine. Any ideas?

Here are the metaboxes:

add_action( 'cmb2_init', 'awc_discog_details' );
function awc_discog_details() {

$prefix = '_awc_';

$cmb = new_cmb2_box( array(
    'id'           => $prefix . 'details_metabox',
    'title'        => __( 'Label / Release Date', 'cmb2' ),
    'object_types' => array( 'awc_discography' ),
    'context'      => 'normal',
    'priority'     => 'high',
) );

$cmb->add_field( array(
    'name' => __( 'Label Name', 'cmb2' ),
    'id' => $prefix . 'label_name',
    'type' => 'text_medium',
) );

$cmb->add_field( array(
    'name' => __( 'Release Date', 'cmb2' ),
    'id' => $prefix . 'release_date',
    'type' => 'text_date',
) );


and this is from the template to display all the info:

$tax_terms = get_terms( 'category', array( 'orderby' => 'id' ) );

foreach ($tax_terms as $tax_term) {

    $args = array(
        'cat'               => $tax_term->term_id,
        'post_type'         => 'awc_discography',
        'posts_per_page'    => '-1',
        'orderby'           => 'ID',

    $query = new WP_Query( $args );
    $nice_class = strtolower($tax_term->name);
    $nice_class = preg_replace("/[\s_]/", "-", $nice_class);

if ( $query->have_posts() ) { ?>

<section class="<?php echo $nice_class; ?> listing">
    <h3><?php echo $tax_term->name; ?>:</h3>

    <?php while ( $query->have_posts() ) {

        $label = get_post_meta( get_the_ID(), '_awc_label_name', true ); 
        $date = get_post_meta( get_the_ID(), '_awc_release_date', true ); 


        <article id="post-<?php the_ID(); ?>" <?php post_class( 'discog large-6 medium-6 small-12 columns' ); ?>>
            <?php if ( has_post_thumbnail() ) { ?>
                <div class="large-4 medium-4 small-12 columns album-cover">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail( 'thumbnail' ); ?>
            <?php } ?>

            <div class="large-8 medium-8 small-12 columns album-content">
              <h4><?php the_title(); ?></h4>
              <?php if ($label) {
                echo '<p><strong>Label: ' . $label . '</strong></p>';
                } ?>
              <?php if ($date) {
                echo '<p><strong>Release Date: ' . $date . '</strong></p>';
                } ?>

              echo '<div class="entry-content">';




    <?php } // end while ?>


<?php } // end if

// Use reset to restore original query.

} // foreach

1 Answer 1


get_the_ID() is returning the wrong ID inside the loop, so you are getting wrong information on every post. On the first post, get_the_ID() will return either false if it is an archive page or the page ID if it is a page. On post two, get_the_ID() will return the ID of post one, so you will get the post meta from post one on post two and so on.

The reason for all of this is, you are trying to get post info and the post ID before the $post global is set to the current post in the loop. Very simple and quick, the_post() sets the $post global to the current post in the loop, so anything done before the the_post() call will have the wrong info and not the info that you expect.

So, to solve your issue, the following lines

$label = get_post_meta( get_the_ID(), '_awc_label_name', true );
$date = get_post_meta( get_the_ID(), '_awc_release_date', true ); 

should be move to after the following line


Also, remember to reset postdata after the loop by adding wp_reset_postdata(); after your endwhile statement

  • boom. thanks Pieter, that did the trick! Turns out I do have wp_reset_postdata(); in my question, but my formatting was incorrect, good catch regardless!
    – alexwc_
    Commented Oct 28, 2015 at 5:14
  • My pleasure. Glad it worked. Enjoy ;-) Commented Oct 28, 2015 at 5:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.