0

I'm trying to add metabox with date in WordPress. I used code from this page

It works well, but I have a problem displaying the metabox values on page.

My loop:

<?php query_posts(array('post_type' => 'event' , 'posts_per_page' => 9999, 'orderby' => 'menu_order', 'order' => 'ASC' ));
if (have_posts()) : while (have_posts()) : the_post();
$custom = get_post_custom($post->ID); ?>

<div class="event">
    <h2><?php the_title(); ?></h2>
    <p>Date: <?php echo get_post_meta($post->ID, '_day', true);?>.<?php echo get_post_meta($post->ID, '_month', true);?>.<?php echo get_post_meta($post->ID, '_year', true);?></p>
    <p>Hour: <?php echo get_post_meta($post->ID, '_hour', true);?>:<?php echo get_post_meta($post->ID, '_minute', true);?></p>
    <?php the_content(''); ?>
</div>
<?php endwhile; endif; ?>

I added custom post type 'event'. Values in metabox(date/location) have been saved, but it haven't displayed on page.

Why it doesn't work?

Thanks for reply.

  • Your code works if the meta key/values are set correctly. The problem has to be elsewhere. And don't ever use query_posts ever. – s_ha_dum Dec 1 '13 at 17:52
  • When I checked the referred link, I found they are using a variable $metabox_id just before the meta key as below $day = get_post_meta( $post->ID, $metabox_id . '_day', true ); But in your case you are directly using the metakey. Please check in the database. If the value is stored correctly, then this might be the issue with your code. – Chittaranjan Dec 1 '13 at 19:06
0

In database, it stores the date details with prefix as _start and _end so you need to change the meta keys accordingly. Please have a look at the below code.

<p>Start time details</p>
<p>Date: <?php echo get_post_meta($post->ID, '_start_day', true);?>.<?php echo get_post_meta($post->ID, '_start_month', true);?>.<?php echo get_post_meta($post->ID, '_start_year', true);?></p>
<p>Hour: <?php echo get_post_meta($post->ID, '_start_hour', true);?>:<?php echo get_post_meta($post->ID, '_start_minute', true);?></p>

<p>End time details</p>
<p>Date: <?php echo get_post_meta($post->ID, '_end_day', true);?>.<?php echo get_post_meta($post->ID, '_end_month', true);?>.<?php echo get_post_meta($post->ID, '_end_year', true);?></p>
<p>Hour: <?php echo get_post_meta($post->ID, '_end_hour', true);?>:<?php echo get_post_meta($post->ID, '_end_minute', true);?></p>

In case $post->ID does not contain a value, try using get_the_ID().

  • Thanks Chittaranjan. I resolved problem after your comment about check DB :) – blam4nz Dec 1 '13 at 20:11

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.