WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used the following query to have only 1 post published on the homepage from a particular category:

<?php if ( have_posts() ) : ?>
<?php query_posts('posts_per_page=1&orderby=date&order=DESC&cat=6'); ?> 

<?php while ( have_posts() ) : the_post(); ?>
   <?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>
<?php endwhile; endif; ?>

Each post has two custom fields containing a begin and an end date. What I need is that after the end date the next post (the one with following begin date) will be shown. But I don't know how to do it.

Any hint?

share|improve this question
Can you explain how you store the date? Is it a string or a timestamp? – Richard Sweeney Nov 11 '12 at 13:32
The 'meta_value' of the custom field in the database is like this: '2012/11/08' so as a string. – Mark Nov 11 '12 at 13:55
What exactly do you mean by "after the end date"? Do you mean "in comparison to the current time"? Do you want to show the post with the closest begin date that lies in the future? Please elaborate on the issue and clarify the situation. – Johannes Pille Nov 11 '12 at 19:26
Yes, I mean if the current time is after the end date (specified in the custom field), it must show the post with the closest begin date that lies in the future. Think about a calendar of shows: I want to show the "Now on the scene" if there is a show going on right now, the "Coming next" to show the closest into the future if there is no show right now. – Mark Nov 11 '12 at 19:40
up vote 1 down vote accepted

Assuming, as per the above comment, the format of the dates is YYYY/MM/DD:

$args = array(
    'posts_per_page' => 1,
    'cat' => 6,
    'meta_key' => 'begin_date',        // adjust to actual key
    'meta_value' => date( 'Y/m/d' ),
    'meta_compare' => '>=',
    'order' => 'ASC',
    'orderby' => 'meta_value'

$wpse72195_query = new WP_Query( $args );

// do something with the result
share|improve this answer
Awesome, I learnt one tag (meta_compare), just another question, there is a way to set a range? – Mark Nov 11 '12 at 22:23
Awesome, I learnt one new tag (meta_compare), just another question, there is a way to set a range? I mean, as I said before if the current time is between begin and end date it should show the post associated with these dates. Right now with your suggestion as soon as the beginning date is passed it switches to the following post. For example: 1) start date (2012/11/10) and end date (2012/11/12); 2)start date (2012/11/22) and end date (2012/11/25), today is the 2012/11/12, it should still show the (1) instead it shows the (2). – Mark Nov 11 '12 at 22:31
With 'meta_key' => 'end_date' it does the job. Thank you :) – Mark Nov 12 '12 at 7:52

Your Answer


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.