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 need to display a list of current tasks. Each task has a custom post_type and a start date and end date.

Can someone tell me how to modify my query so it will pick up any tasks that have a start date in the past and an end date in the future? (There's another meta_key named 'end_date')

Here's my query:

$todays_date = time();

$args = array ('post_type'      => 'tasks',
               'meta_key'       => 'start_date',
               'meta_compare'   => '>',
               'meta_value'     => $todays_date,
               'orderby'        => 'meta_value'
              );

$tasks = get_posts($args);

The problem with the query above is that it's showing tasks that in the future, but haven't started yet.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

change your query a bit:

$todays_date = time();

$args = array ('post_type'      => 'tasks',
               'meta_key'       => ''end_date,
               'meta_compare'   => '<',
               'meta_value'     => $todays_date,
               'orderby'        => 'meta_value'
              );

$tasks = get_posts($args);

and then for each one of this posts get the start_date meta and compare with $today something like:

$today = strtotime($today);
foreach ($tasks as $task_post){
  $start = strtotime(get_post_meta($task_post->ID, start_date, true));
  if ($today > $start){
    //to task stuff here
  }
}

Hope this helps.

share|improve this answer
    
@בניית אתרים: I can't seem to get it to work. I noticed you had a typo -- strtorime instead of strtotime. I tried to edit your post but it said I didn't change enough characters or something lol. Did you mean $today = strtorime($todays_date); ($todays_date instead of $today)? Not sure if this is the problem, but the way the start_date is stored in my database appears like "1292549400", which is February 3, 2011 at 12:00 AM CST. –  user1462 Feb 4 '11 at 18:34
    
I fixed it. As for your start_date is already stored as integer then you can skip the strtotime. Any way when I'll get home i will look at it again. –  Bainternet Feb 4 '11 at 20:03
    
@בניית אתרים No worries. I got it working. Thanks for your help! –  user1462 Feb 4 '11 at 20:35
add comment

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.