I'm making a custom activity calendar for a client here: http://arbeidshesten.com/aktivitetskalender/

The backend is a custom post type "aktiviteter", with custom fields for the date ("dato") and some more details.

The code looks like this:



 <?php $loop = new WP_Query( array( 
'post_type' => 'aktiviteter', 
'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))); ?>

<td><?php the_title( '<a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a>' ); ?></td>
<td><?php echo get_post_meta($post->ID, "sted", true); ?></td>
<td><?php echo $display_date; ?></td>
<td><?php echo get_post_meta($post->ID, "kontaktperson", true); ?></td>

<?php endwhile; ?>


Funny thing is that the query returns just 2 aktivities, even though I entered (and published) about 5 future activities.

Anyone a clue?


I wonder if adding 'posts_per_page' => XXX to your query would give the desired results? Just a thought. Good luck!


My best guess is that if you are running WP 3.1, the meta_key, meta_value, and meta_compare parameters of the WP_Query is deprecated. In 3.1 and above, the meta_query parameter is what you should use. This is an awesome addition to WP_Query as it allows for query multiple meta values. Your query should look like:

    $loop = new WP_Query( 
            'post_type' => 'aktiviteter', 
                'meta_query' => array(
                    'key' => 'dato',
                    'value' => date('Y-m-d'),
                    'compare' => '>=',
                    'type' => 'DATE'
            'orderby' => 'dato',
            'order' => 'ASC'




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.