Accessing post custom fields in WordPress sidebar widget

I’m building a simple sidebar widget to display a loop of a custom post type “shows”.

Each “show” has about 3 custom fields, which i’d like to output via the loop as well. This is the code i’m using:

Read More

This is the loop within my plugin code:

<?php
// WIDGET CODE GOES HERE
$args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    $month = get_post_meta($post->ID,'month-abbreviation',true);
    $date = get_post_meta($post->ID,'date',true);
    $citystate = get_post_meta($post->ID,'city-state',true); ?> 
    <article class="sidebar-show clearfix">
        <a class="show-link" href="<?php the_permalink(); ?>">
            <div class="date-box">
                <span class="month"><?php echo $month; ?></span>
                <span class="date"><?php echo $date; ?></span>
            </div>
            <div class="venue-box">
                <?php echo "<h4>".get_the_title()."</h4>"; ?>
                <?php echo "<p>".$citystate."</p>"; ?> 
            </div>
        </a>
    </article>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();
?>
<?php 
    echo $after_widget;
  }

}
add_action( 'widgets_init', create_function('', 'return register_widget("ShowsSidebarWidget");') );?>

This code pulls in the post titles, but doesn’t display the custom fields month-abbreviation, date, and city-state.

What’s missing here?

EDIT: Removed double quotes after avexdesign’s reply.

Related posts

Leave a Reply

3 comments

  1. Ok, I see a few things you can try.

    1. month-abbreviation – should be single quotes. Like ‘month-abbreviation’

    2. Try taking out: $month=  $date= and $citystate =

    3. Might need echo

    So Try:

    echo get_post_meta($post->ID,'month-abbreviation', true);

    echo get_post_meta($post->ID,'date', true);

    echo get_post_meta($post->ID,'city-state', true);

  2. Try something like:

    <?php $loop = new WP_Query( array( 'post_type' => 'shows', 'posts_per_page' => 10 ) ); ?>

    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <span class="month"><?php echo get_post_meta($post->ID, 'month-abbreviation', true); ?></span>

    <span class="date"><?php echo get_post_meta($post->ID, 'date', true); ?></span>

    and so on… for your other custom IDs