Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to fetch a (date) meta field from a custom post type in a sidebar widget here: http://www.oransje.com/utvikling/ngnf/kurs/

Somehow the date doesn't show correctly inside the widget (everywhere else I put it, also manually in the sidebar the code works fine). Anyone a clue?

The code looks like this:

<?php $loop = new WP_Query(array( 
'post_type' => 'kurs', 
'posts_per_page' => '5',
'meta_key' => 'dato',
'meta_value' => date('Y-m-d'),
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
 )); ?>

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

<?php $display_date = date('d.m.Y', strtotime(get_post_meta($post->ID, "dato", true))); ?>

<li><?php echo $display_date; ?> - <?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a>' ); ?></li>

<?php endwhile; ?>
</ul>
share|improve this question
    
I fixed this by adding: global $post; –  Tom Boersma Sep 15 '12 at 5:51
1  
Please add this as answer. –  kaiser Sep 15 '12 at 10:50
1  
"Users with less than 10 reputation can't answer their own question for 8 hours after asking. You may self-answer in 1 hour. Until then please use comments, or edit your question instead." –  Tom Boersma Sep 15 '12 at 11:02

1 Answer 1

up vote 0 down vote accepted

I fixed this by adding:

global $post;

More specific: I can't explain why this works, that's beyond my WordPress skills, but I added it right after the start of the query, so the code looks like this:

<?php global $post; 
$loop = new WP_Query( array( 
'post_type' => 'kurs', 
'posts_per_page' => '5',
'meta_key' => 'dato',
'meta_value' => date('Y-m-d'),
'meta_compare' => '>=',
'orderby' => 'meta_value',
'order' => 'ASC'
 ) ); ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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