dirk
dirk

Reputation: 2306

Complex wordpress query with conditional custom field

I'm trying to show a calendar in my sidebar. Posts that should show up in this calendar have a custom field called event_date. The field is populated with a date, formatted like YYYYMMDD. I try to make just events in the future show up.

My first plan, to only show posts with the event_date field, is succeeded:

<?php
$args = array
  (
  'showposts' => 10,
  'meta_key' => 'event_date',
  'meta_value' => '',
  'meta_compare' => '!='
  );
query_posts( '$args' );
?>

But I have no idea how to proceed from here and only filter out the events in the future. Ofcourse I can filter later in the while loop, but that doesn't sound like a clean solution.

If it's possible to sort them by date that would be even more amazing, but otherwise I'll just trow everything in an array and calculate the order from there.

Using a timestamp instead of yyyymmdd is not a solution, as that will require a lot of education to the editors.

Hopefully someone can point me in the right direction. Many thanks in advance!

Upvotes: 1

Views: 421

Answers (1)

dirk
dirk

Reputation: 2306

Typing out your question always helps. I got it! Thanks for reading anyways!

Here is my solution, stupid that I didn't figure this out before:

<?php
$args = array
(
'showposts' => 10,
'meta_key' => 'event_date',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_value' => date("Ymd", date("U")-3600*24),
'meta_compare' => '>'
);
query_posts( $args );
?>

Upvotes: 2

Related Questions