1

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?

4
  • Can you explain how you store the date? Is it a string or a timestamp? Commented Nov 11, 2012 at 13:32
  • The 'meta_value' of the custom field in the database is like this: '2012/11/08' so as a string.
    – Mark
    Commented Nov 11, 2012 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. Commented Nov 11, 2012 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
    Commented Nov 11, 2012 at 19:40

1 Answer 1

1

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
3
  • Awesome, I learnt one tag (meta_compare), just another question, there is a way to set a range?
    – Mark
    Commented Nov 11, 2012 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
    Commented Nov 11, 2012 at 22:31
  • With 'meta_key' => 'end_date' it does the job. Thank you :)
    – Mark
    Commented Nov 12, 2012 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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