views:

170

answers:

1

Working in WordPress here. As you may know, all of the typical solutions for displaying upcoming events in WordPress are awful, so I'm accomplishing it with simple custom fields. Each event the user will enter they will enter a Numeric date "YYYYMMDD".

What I need to find out is how to code in PHP for the script to first get php:the_date <'YYYYMMDD'> and then have WordPress filter events dynamically, only displaying posts that have a value greater than or equal to the numeric value printed (which would be the YYYYMMDD format of "today" which is pulled in via PHP:the_date.

Here is the wp_query I'm using:

    <?php
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5&meta_key=event_date&orderby=meta_value&category_name=events&order=ASC');       
    if ($recentPosts->have_posts()) : while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
+1  A: 

To get the date: $current_date = date('Ymd')

You should then be able to query your posts something like this:

$recentPosts = new WP_Query();
$recentPosts->query(array(
    'category_name' => 'events',
    'meta_key' => 'event_date',
    'meta_compare' => '>=',
    'meta_value' => $current_date,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'posts_per_page' => 5
));

Then you can loop through the posts as normal. If you need to do pagination it will be a bit more complex, see to the documentation for query_posts.

Richard M
Thanks so much! I edited the question above to show the WordPress Query I'm using. It's sorting by the category "events" ordered "ascending" and showposts is set to "5".
Brian
I'm not sure if you needed it, but I've updated my answer with the parameters from your query.
Richard M
Thanks so much Richard! I did need it, I am SOOO grateful for the help of all the StackOverflowers I'm more of a designer than a developer and I am so happy for all the kind people like you!!!
Brian